Controller 'alertForm', required by directive 'loginForm', can't be found!
angular.module('jobsApp')
.directive('alertForm', function () {
return {
templateUrl: 'app/directives/alert/alertForm.html',
restrict: 'E',
scope: {
topic: '=topic',
description: '=description'
}
}
})
.directive('loginForm', function() {
return {
templateUrl: 'app/directives/loginForm/loginForm.html',
restrict: 'E',
require: '^alertForm',
scope: {
successCallback: '&',
errorCallback: '&',
emailField: '='
},
link: function (scope, element, attrs, alertFormModelCtrl) {
scope.alertFormModel = alertFormModel
},
controller: function ($rootScope, $scope, authenticationService) {
$scope.loginFormData = {};
$scope.inProgress = false;
$scope.onLogin = function (form) {
if (form.$valid) {
$scope.inProgress = true;
authenticationService.loginUser('password', $scope.loginFormData).then(function () {
$scope.inProgress = false;
}, function (err) {
$scope.inProgress = false;
if (err.message) {
//$scope.alertFormCtrl.topic = "asdffasfd";
alert(err.message);
}
});
}
}
}
};
});
最佳答案
您的指令要求 alertForm
Controller 出现在 loginForm
的范围内。您必须将其添加到当前范围。或者在要求前加上 ^
前缀以在父范围中搜索。
来自https://docs.angularjs.org/guide/directive :
The
myPane
directive has arequire
option with value^myTabs
. When a directive uses this option, $compile will throw an error unless the specified controller is found. The ^ prefix means that this directive searches for the controller on its parents (without the^
prefix, the directive would look for the controller on just its own element).
关于javascript - 指令 'alertForm' 所需的 Controller 'loginForm' 无法找到!在 AngularJS 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33775699/