简体   繁体   English

AngularJS指令隔离范围不更新父级

[英]AngularJS Directive Isolate Scope Not Updating Parent

I've got a directive that has a model bound 2-ways, when calling a save() method via ng-click the parent scope isn't updated unless I call $scope.$apply() which then throws the $apply already in progress error. 我有一个带有模式绑定2路的指令,当通过ng-click调用save()方法时,父作用域不会更新,除非我调用$ scope。$ apply()然后抛出$ apply进行中错误。

I'm using ngResource, and the event has a listener calling $scope.model.$save(); 我正在使用ngResource,并且该事件有一个监听器调用$ scope.model。$ save();

Is there a work-around for this? 有解决办法吗? Or am I doing something completely wrong? 或者我做错了什么?

.directive('editable', function(){
    return {
        restrict: 'AE',
        templateUrl: '/assets/partials/editable.html',
        scope: {
            value: '=editable',
            field: '@fieldType'
        },
        controller: function($scope){

           ...

            $scope.save = function(){
                $scope.value = $scope.editor.value;
                $scope.$emit('saved');
                $scope.toggleEditor();
            };

        }
    };
})

UPDATE UPDATE

It looks like it is updating the parent after all but that the emit is being fired before the digest has finished completing. 看起来它毕竟更新了父节点,但是在摘要完成之前发射了一些内容。 I can force it to the end of the stack using $timeout but it feels a bit hacky. 我可以使用$ timeout强制它到堆栈的末尾,但感觉有点hacky。 Is there a better way? 有没有更好的办法?

$scope.$on('saved', function(){
    $timeout(function(){
        $scope.contact.$update();
    }, 0);
});

How are you calling $scope.save ? 你怎么称$ scope.save? If you use one of the angular directives, like ng-click, angular will automatically run a digest cycle for you and therefore you don't need to call $scope.$apply(). 如果你使用其中一个angular指令,比如ng-click,angular会自动为你运行一个摘要周期,因此你不需要调用$ scope。$ apply()。 Internally, angular checks if a digest is already in progress before starting another cycle, so it will handle the issue of digest already in progress for you if you use one of the built-in directives. 在内部,角度检​​查在开始另一个循环之前是否已经在进行摘要,因此如果您使用其中一个内置指令,它将处理您正在进行的摘要问题。 For example, put this in your directive's template... 例如,将它放在指令的模板中......

<button ng-click="save()">Save</button>

If you need to call save from the controller or link function, you can do a little hack to prevent the 'digest already in progress' error. 如果您需要从控制器或链接功能调用save,您可以进行一些破解以防止“正在进行摘要”错误。 Use the $timeout service to defer the call to the end of the call stack... 使用$ timeout服务将调用推迟到调用堆栈的末尾...

$scope.save();
$timeout(function() {
    $scope.$apply();
}, 0);

We are setting the timeout to 0, so there is no real delay, but this is still enough to push the $apply call to the end of the current call stack, which allows the digest that is in progress to finish first and prevents the error. 我们将超时设置为0,因此没有实际延迟,但这仍然足以将$ apply调用推送到当前调用堆栈的末尾,这允许正在进行的摘要首先完成并防止错误。 This is not ideal and could imply a larger issue with your design, but sometimes you just have to make it work 这并不理想,可能意味着您的设计存在更大的问题,但有时您只需要使其工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM