Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS:更改自定义指令中的父范围值_Angularjs_Angularjs Directive - Fatal编程技术网

AngularJS:更改自定义指令中的父范围值

AngularJS:更改自定义指令中的父范围值,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我是AngularJS的新手。我希望实现一个支持“警报”(或说“通知”)的自定义指令,例如,当我成功更新用户信息时,页面上将出现一个小框,上面写着“您的信息已成功更新”,并在大约2秒钟后消失 下面是我当前的代码(我正在使用引导模板,因此类“alert”就是为了这个目的): 在notification.jade中(我使用jade作为模板引擎): 我想做的事情如下。在父$scope中,我有一个名为“submissionSuccess”的变量,当对后端的Ajax调用成功时,它被设置为true。如果为t

我是AngularJS的新手。我希望实现一个支持“警报”(或说“通知”)的自定义指令,例如,当我成功更新用户信息时,页面上将出现一个小框,上面写着“您的信息已成功更新”,并在大约2秒钟后消失

下面是我当前的代码(我正在使用引导模板,因此类“alert”就是为了这个目的):

在notification.jade中(我使用jade作为模板引擎):

我想做的事情如下。在父$scope中,我有一个名为“submissionSuccess”的变量,当对后端的Ajax调用成功时,它被设置为true。如果为true,我将显示通知栏(因此在Jade文件中显示ng show='condition'),并且我希望在一段时间后隐藏此栏(这就是为什么我在指令配置中有$timeout片段)。然而,似乎有一些bug。我无法在自定义指令中更改父$scope.condition的值,因此通知栏不会自动隐藏


我应该如何解决这个问题?非常感谢您的帮助。

这就是基于原型的范围继承的工作原理。在指令实例化期间,指令必须在隔离范围内创建自己的
条件
属性。它在这里与父作用域没有连接,因为它是在子作用域中定义的私有值(true/false)。这与基本值类似于父原型属性的“阴影”这一事实有关

我建议坚持使用更全面、更可靠的对象引用:

.directive('notification', ['$timeout',
    function($timeout) {
        return {
            restrict: 'E',
            scope: {
                config: '='
            },
            replace: true,
            transclude: true,
            link: function(scope, element, attributes) {
                scope.$watch('config.condition', function() {
                    if (scope.config && scope.config.condition) {
                        $timeout(function() {
                            scope.config.condition = false; // I wish to change the value in the parent scope
                        }, 2000);
                    }
                });
            },
            templateUrl: 'partials/notification.html'
        };
    }
]);
然后模板将变为:

<div class="alert alert-{{ config.type }}" ng-transclude="" ng-show="config.condition"></div>


这里有消息。
演示:
notification(type='"success"', condition='submissionSuccess')
  span Your information has been successfully updated.
.directive('notification', ['$timeout',
    function($timeout) {
        return {
            restrict: 'E',
            scope: {
                config: '='
            },
            replace: true,
            transclude: true,
            link: function(scope, element, attributes) {
                scope.$watch('config.condition', function() {
                    if (scope.config && scope.config.condition) {
                        $timeout(function() {
                            scope.config.condition = false; // I wish to change the value in the parent scope
                        }, 2000);
                    }
                });
            },
            templateUrl: 'partials/notification.html'
        };
    }
]);
<div class="alert alert-{{ config.type }}" ng-transclude="" ng-show="config.condition"></div>
<notification config="config">
    Some message here.
</notification>