0
我正在從下面的網站學習angular.js,http://docs.angularjs.org/guide/dev_guide.services.injecting_controllers如何通過angular.js將服務注入控制器?
我試圖在我的本地服務器上運行代碼片段。但它失敗了。 我認爲問題在於控制器不知道'通知'工廠。 有人可以幫助糾正代碼?謝謝。
HTML
<body ng-app="myApp">
<div id="simple" ng-controller="myController">
<p>Let's try this simple notify service, injected into the controller...</p>
<input ng-init="message='test'" ng-model="message" >
<button ng-click="callNotify(message);">NOTIFY</button>
<p>(you have to click 3 times to see an alert)</p>
</div>
JS
<script>
//MyServiceModule service module
angular.module('MyServiceModule', []).factory('notify', ['$window', function(win) {
var msgs = [];
return function(msg) {
msgs.push(msg);
if (msgs.length == 3) {
win.alert(msgs.join("\n"));
msgs = [];
}
};
}]);
//Controller
function myController(scope, notifyService) {
scope.callNotify = function(msg) {
notifyService(msg);
};
}
myController.$inject = ['$scope','notify'];
// How to let the myController know the 'notify' factory in MyServiceModule?
//myApp
angular.module('myApp', [])
.controller('myController',myController);
</script>