2

私はこの問題を何時間も研究してきましたが、最終的に plunker で再現しました

これが私の問題です:

外部リソースをテンプレートとして使用するカスタマイズされたディレクティブを ng-repeat と組み合わせると、モデルが変更されている間、ビューが正しくレンダリングされませんでした。

私の例では、リンクをクリックするとモデルが置き換えられますが、古いデータは消去されていません。

template: 'stringTemplate'の代わりに使用するとtemplateUrl: 'urlToTemplate'、正常に動作します。バグなのかどうなのかはまだわかりませんが…

部分的なコード:

angular.module('test', [])
    .run(function($rootScope) {
        $rootScope.topics = [{
            content: 'Click here to change reply',
            replys: [{
                content: 'Reply test...',
            }]
        }];
    })
    .directive('topic', function() {
        return {
            replace: true,
            restrict: 'E',
            templateUrl: 'topic.htm',
            link: function(scope) {
                scope.reply = function(input) {
                    scope.topic.replys = [{ content: '"Reply test..." should be replaced, but it\'s not!' }];
                }
            }
        };
    })
    .directive('reply', function() {
        return {
            replace: true,
            restrict: 'E',
            // template: '<div><div ng-bind="reply.content"></div></div>' //this works fine
            templateUrl: 'reply.htm' // same content
        };
    });
4

1 に答える 1

1

私はいくつかの調査を行いましたが、この問題はあなただけではないようです:

https://github.com/angular/angular.js/issues/2151

ユーザーishwは、簡単な修正として次のように述べています。

「まだ気づいていないかもしれない人のために: ng-repeat がディレクティブのテンプレートのルート要素にあるからです。ng-repeat を任意の要素にラップすれば問題ありません。」

私はあなたのplunkrでこれを試しましたが、うまくいっているようです:

  <div> 
      <div class="topic" ng-bind="topic.content" ng-click="reply()"></div>
      <div ng-repeat="reply in topic.replys"><reply></reply></div>
  </div>
于 2013-10-04T10:00:47.540 に答える