angularjs – 如何从一个角度模块传递另一个模块的值?
发布时间:2020-12-17 17:56:07 所属栏目:安全 来源:网络整理
导读:Iam New To Angular Concept Iam试图将值从一个模块传递到另一个模块. 我使用Meanjs Generator根据需要为每个模块添加了更多模块. 示例: – 餐厅模块的餐厅控制器: – angular.module('restaurants').controller('RestaurantController',['$scope','$http'
Iam New To Angular Concept Iam试图将值从一个模块传递到另一个模块.
我使用Meanjs Generator根据需要为每个模块添加了更多模块. 示例: – angular.module('restaurants').controller('RestaurantController',['$scope','$http','$stateParams','$location','Authentication','$rootScope' function($scope,$http,$stateParams,$location,Authentication,$rootScope) { $scope.create = function() { // Create new Restaurant object var resObj = { displayName: this.displayName,var restaurant = new Restaurants(resObj); // Redirect after save restaurant.$save(function(response) { $location.path('restaurants/'+ restaurant._id); **Need to Pass restaurant._id to the Menusconttoller** },function(errorResponse) { $scope.error = errorResponse.data.message; }); }; ]); 我需要将餐馆ID传递给菜单控制器,这可能吗? angular.module('menus').controller('MenusController1','Authentication' function($scope,Authentication) { // Create new Menu $scope.create = function () { $scope.isCreateloading=true; var menusObj= { 'displayName' : this.displayName restaurantId : this.restaurantId // **where i need to recieve the Id which is pased from Restaurant Controller** }; here i need to get the Id from Restaurant Module $scope.menuForm=menusObj; var menu = new Menus1(menusObj); // Redirect after save menu.$save(function (response) { $location.path('menus/'+menu._id); },function (errorResponse) { $scope.error = errorResponse.data.message; }); }; ]); 我做了$rootscope Broadcasting,$scope.emit等,但是所有的例子都在同一个模块中有更多的控制器.哪个不适合我的场景. 请建议: – **如何将id从一个模块传递到另一个模块?** 解决方法
你可以这样做:
//this is one module var myUtilModule = angular.module("myUtilModule",[]); // this is value to be shared among modules,it can be any value myUtilModule.value ("myValue","12345"); //this is another module var myOtherModule = angular.module("myOtherModule",['myUtilModule']); myOtherModule.controller("MyController",function($scope,myValue) { // myValue of first module is available here } 这是教程AngularJS Modularization & Dependency Injection 快乐帮助! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |