gpt4 book ai didi

angularjs - 如何从一个 Angular 模块传递另一个模块的值?

转载 作者:行者123 更新时间:2023-12-04 22:45:20 27 4
gpt4 key购买 nike

我是 Angular 概念的新手我正在尝试将值从一个模块传递到另一个模块。
我使用 Meanjs Generator 根据要求为每个模块添加了更多模块。

例子 :-
餐厅模块的餐厅 Controller :-

 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 传递给下面的菜单 Controller 这可能吗?
angular.module('menus').controller('MenusController1', ['$scope','$http', '$stateParams', '$location', 'Authentication'
function($scope,$http, $stateParams, $location,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 等,但所有示例在同一模块中都有更多 Controller 。这不适合我的场景。

请建议:-

** 如何将 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

乐于助人!

关于angularjs - 如何从一个 Angular 模块传递另一个模块的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29432943/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com