我得到Uncaught Error: Unknown provider: testProvider from myApp
在下面的代碼:角:錯誤:module.config在未知的提供()
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
+1維基鏈接 – Martijn
來到這裏尋找如何讓注入配置調用模塊上的價值,發現在維基鏈接答案。我以前讀過配置塊只能用提供者和常量注入,我以前從來沒有聽說過常量。我只是假設他們沒有價值觀。感謝您的鏈接! – kelv
注意提供程序註冊名稱('test'),配置時的名稱('testProvider')和調用名稱('test')之間的區別。我不記得在API中閱讀。如果參考文獻出現,我會在這裏發佈。 –