javascript - 指令 'alertForm' 所需的 Controller 'loginForm' 无法找到!在 AngularJS 中

标签 javascript angularjs

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 a require 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/

相关文章:

javascript - rails : where to enable Access-Control-Allow-Origin

javascript - 窗口单击事件 Javascript 在第一次单击时不起作用

javascript - 如何使用 JavaScript 从自定义字符中提取字符串?

javascript - 通过 npm 下载 system-polyfills.js

javascript - AngularJS errors.angularjs.org - 错误文档链接出现 404 错误

javascript - 添加::-moz-focus-inner 使用镭

javascript - Android 版 Chrome 上的 Html 文件输入缺少扩展名和 mime 类型

javascript - Ionic - 从侧面菜单重新启动时 View 不刷新

javascript - 自定义指令内 ngDirectives 的奇怪行为

javascript - AngularJS 中的通用 getterSetter