当我从 View 中删除 ng-controller 时,resolve 属性工作得很好,但是当我将 ng-controller 放入模板中时,它会抛出未知的提供程序。任何人都可以帮我解决为什么会发生这种情况吗?
app.config(['$routeProvider',function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'page/login.html',
controller: 'loginController',
resolve: {
message: function(demoService){
return demoService.getResolveContent();
},
greeting: function(demoFactory){
return demoFactory.getGreeting();
}
}
}).
when('/profile', {
templateUrl: 'page/profile.html',
controller: 'profileController'
})
.otherwise({
redirectTo: '/'
});
}]);
function loginController($scope,$location,message,greeting){
$scope.tempfactory = greeting; }
<div style="color: white;width: 100%;height: 100%;background-color: lightgray" ng-controller="loginController">
<p>{{tempfactory}}</p>
<input type="text" ng-model="inputBoxValue">
<button style="width: 100px;height: 100px" ng-click="profilePage()"></button>
</div>
如果我用这个替换上面的 html:
<div style="color: white;width: 100%;height: 100%;background-color: lightgray" >
<p>{{tempfactory}}</p>
<input type="text" ng-model="inputBoxValue">
<button style="width: 100px;height: 100px" ng-click="profilePage()"></button>
</div>
它运行得非常好。任何人都可以告诉我其中的原因吗?
最佳答案
使用resolve
添加的依赖项将作为正在解析的路由和正在创建的 Controller 的一部分注入(inject),否则不可用。这些不是像 service
或 filter
这样的全局依赖项。
如果您使用ng-controller
,则不会发生路由转换,并且不会发生与路由相关的依赖项解析和 Controller 创建,并且会引发 DI 错误。
如果将 Controller 放入路由定义中并使用 ng-controller
,则会创建两个 Controller 实例,但其中一个会出现 DI 错误。
关于javascript - Angular js :-> angular throws unknown provider error while accessing resolve proprerty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26775611/