我得到Uncaught Error: Unknown provider: testProvider from myApp
下面的代码:
test
是一个自定义提供者。
angular.module('myApp', [])
.config(function (testProvider) {
testProvider.setPrefix("works: ");
});
完整代码在这里:
angular.module('myApp', [])
.config(function (testProvider) {
testProvider.setPrefix("works: ");
});
angular.module('myApp')
.provider ("test", function () {
var prefix;
this.setPrefix = function(p) {
prefix = p;
}
this.$get = function () {
return {
log: function(msg) {
console.log (prefix + msg);
}
}
}
});
angular.module('myApp')
.controller ("myCtrl", function($scope, test) {
$scope.$watch ('myModel', function (newval) {
test.log(newval);
})
});
Plunker 链接:http ://plnkr.co/edit/zcIHRn?p=preview