私はこの単純な jsfiddle を書き、ng-repeat のディレクティブで基本的な addClass を (成功して) 実行しました。
さて、私の質問は次のとおりです。そのようなDOM操作を行うのに最適な(または意図された)場所は次のとおりです。
A.指令で?
B. コントローラーで?
私の例では、両方の可能性が示されています。
コード:
var TestApp = angular.module("TestApp", ['ngResource']);
TestApp.directive('onLoad', function() {
return {
restrict: 'A',
link: function(scope, elm, attrs) {
elm.addClass('loaded'); // A: DOM manipulation in directive
scope.initMe(scope.$eval(attrs.onLoad2), elm); // B: DOM manipulation handled in controller
}
};
});
前もって感謝します :)