我尝试使用angular-ui,并尝试注入$ stateProvider:
HTML
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular-resource.min.js"></script>
<script src="http://angular-ui.github.io/ui-router/release/angular-ui-router.min.js"></script>
<script src="test/appModule.js"></script>
</head>
<body>
<div ng-app="appModule">
<div ng-controller="appController">
{{date}}
</div>
</div>
</body>
</html>
js(test / appModule.js)
var module = angular.module("appModule", ['ui.router']);
module.controller('appController', ['$scope', '$stateProvider',
function ($scope, $stateProvider) {
$scope.date = new Date();
}]);
堆栈跟踪
Error: Unknown provider: $stateProviderProvider <- $stateProvider
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:28:236
...
如果我删除带有评论的 $ stateProvider 和 ui.router ,一切都会有效:
var module = angular.module("appModule"/*, ['ui.router']*/);
module.controller('appController', ['$scope'/*, '$stateProvider'*/,
function ($scope, $stateProvider) {
$scope.date = new Date();
}]);
那么注入 $ stateProvider 有关解决的任何想法的问题?
P.S。我试过ui sample它有效,但我无法弄清楚为什么我没有。
答案 0 :(得分:32)
在控制器中使用它时,您必须使用$state
:
angular.module("appModule", ['ui.router']).controller('appController', ['$scope', '$state', function ($scope, $state) {
$scope.date = new Date();
}]);
您只能在配置中使用状态提供程序,例如:
angular.module('appModule').config(['$stateProvider', function($stateProvider){
/* do w/e with state provider */
})];
答案 1 :(得分:0)
var bootstrapApp = angular.module('bootstrapDemoApp', ['ui.bootstrap','ui.router']);
bootstrapApp.config(function($stateProvider, $urlRouterProvider,$locationProvider) {
// For any unmatched url, redirect to /
$urlRouterProvider.otherwise("/");
// Now set up the states
$stateProvider
.state('login', {
url: "/",
controller: "loginCtrl",
templateUrl: "partials/login.html"
})
});