Hajder离开this comment,并在其中,他说:
如果您使用的路线(大概率)和你的配置有一个参考一个未声明为依赖项的模块中的控制器,则初始化可能会失败。
比如假设你已经为你的应用程序配置ngRoute,像
angular.module('yourModule',['ngRoute'])
.config(function($routeProvider, $httpProvider) { ... });
在声明路由块小心,
.when('/resourcePath', {
templateUrl: 'resource.html',
controller: 'secondModuleController' //lives in secondModule
});
申报secondModule为后“ngRoute”应该解决这一问题的相关性。我知道我有这个问题。
即使有了这个帮助,我花了一分钟的时间才得到它的工作,所以我想我会在这里分享我的示例代码,以帮助下一个可怜的混蛋卡住这个。
首先,在我宣布我的路线地点:
var app = angular.module('sporkApp', ['ngRoute','validationApp']);
app.config(function ($routeProvider) {
$routeProvider
.when('/home',
{
controller: 'HomeController',
templateUrl: 'home/home.template.html'
})
.when('/tags',
{
controller: 'TagsController',
templateUrl: 'tags/tags.template.html'
})
.when('/test',
{
controller: 'mainController',
templateUrl: 'test/test.template.html'
})
.otherwise({ redirectTo: '/home' });
});
然后,你需要的地方添加控制器代码,在那里将被装载在你的shell页:
// create angular app
var validationApp = angular.module('validationApp', []);
// create angular controller
validationApp.controller('mainController', function($scope) {
// function to submit the form after all validation has occurred
$scope.submitForm = function() {
// check to make sure the form is completely valid
if ($scope.userForm.$valid) {
alert('our form is amazing');
}
};
});
最后,您需要将相应的ng-app
和ng-controller
添加到包装您要验证的控件的某个页面元素。我把下面的div标签放在里面:
<div ng-app="validationApp" ng-controller="mainController">