gpt4 book ai didi

javascript - angular ng-app Controller 注入(inject)

转载 作者:行者123 更新时间:2023-11-29 10:19:55 26 4
gpt4 key购买 nike

我在为广播服务注入(inject) Controller 时遇到问题...
我找到了这个工作教程 http://jsfiddle.net/simpulton/GeAAB/

但是我有一个这样封装的 Controller (myApp)

myApp.controller('ControllerZero', 
function ControllerZero($scope, sharedService) {
$scope.handleClick = function(msg) {
sharedService.prepForBroadcast(msg);
};

$scope.$on('handleBroadcast', function() {
$scope.message = sharedService.message;
});
});

我的问题是.. 我不知道如何像之前的教程那样注入(inject) Controller

如果我把这个 inject 放在我的 Controller 下

ControllerZero.$inject = ['$scope', 'mySharedService'];

这让我回到控制台:

Uncaught ReferenceError: ControllerZero is not defined

最佳答案

你需要使用一个数组让angular知道所有的 Controller 变量

myApp.controller('ControllerZero', ['$scope', 'mySharedService',  
function ControllerZero($scope, sharedService) {
$scope.handleClick = function(msg) {
sharedService.prepForBroadcast(msg);
};

$scope.$on('handleBroadcast', function() {
$scope.message = sharedService.message;
});
}]);

关于javascript - angular ng-app Controller 注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13991213/

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