简体   繁体   English

AngularJs指令视图未在ng-repeat上更新

[英]AngularJs directive view not updating on ng-repeat

I have directive with ng-repeat inside. 我有ng-repeat里面的指令。 And I have toggle function inside that changing order of array items. 而且我在数组项的更改顺序中具有切换功能。 When I'm displaying scope model I see that items changing their order but UI not updating. 当我显示范围模型时,我看到项目改变了它们的顺序,但UI没有更新。 Tried scope.$apply() but it says that process busy. 尝试了scope。$ apply(),但它表示该进程很忙。

.directive('example', function() {
    return {
        restrict: 'E',
        transclude: true,
        template: `
                Checking model: {{model}}

                <div ng-repeat="(key, row) in model">
                        <div ng-hide="key == 0">
                            <div class="icon-arrow-u" ng-hide="$first" ng-click="moveUp(key)"></div>
                            <div class="icon-delete" ng-click="removeCell(key)"></div>
                            <div class="icon-arrow-d" ng-hide="$last" ng-click="moveDown(key)"></div>
                        </div>
                    </div>
                </div>
        `,
        scope: {
            model: '='
        },
        link: function(scope, element) {
            scope.moveItem = function (origin, destination) {
                var temp = scope.model[destination];
                scope.model[destination] = scope.model[origin];
                scope.model[origin] = temp;
            };

            scope.moveUp = function(index) {
                scope.moveItem(index, index - 1);
            }

            scope.moveDown = function(index) {
                scope.moveItem(index, index + 1);
            }
        }
    }
})

Without seeing a code fiddle, I'd guess that the reason lies within the ng-repeat directive. 在没有看到代码摆弄的情况下,我猜想原因在于ng-repeat指令之内。 You should add a track by expression (see AngularJS doc ) 您应该track by表达式添加track by (请参阅AngularJS doc

For example you could write (key, row) in model track by key or (key, row) in model track by row . 例如,你可以写(key, row) in model track by key(key, row) in model track by row Either way it has to be a value that's unique to every row. 无论哪种方式,它都必须是每一行唯一的值。

Scope is isolated when a directives is used. 使用指令时,作用域是隔离的。 You need to apply the scope to see the changes. 您需要应用范围以查看更改。

Add the following code on each function in the link or whenever the event is called. 在链接中的每个函数上或在调用事件时,都添加以下代码。

Inside link function use : 内部链接功能使用:

        if (scope.$root.$$phase != '$apply' &&
            scope.$root.$$phase != '$digest') {
            scope.$apply();

        }

From main controller 从主控制器

        if ($scope.$root.$$phase != '$apply' &&
            $scope.$root.$$phase != '$digest') {
            $scope.$apply();

        }

if that doesn't work, try calling this on each event. 如果这不起作用,请尝试在每个事件上调用它。

function apply(scope) {
  if (!scope.$$phase && !scope.$root.$$phase) {
    scope.$apply();
    console.log("Scope Apply Done !!");
  } 
  else {
    console.log("Scheduling Apply after 200ms digest cycle already in progress");
    setTimeout(function() {
        apply(scope)
    }, 200);
  }
}

Hope this helps. 希望这可以帮助。

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

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