angularjs - 動的クラスを持つ要素を見つける
-
20-12-2019 - |
質問
NGクラスを使用してクラスを追加していますが、ディレクティブを使用してこれらの要素を見つけようとすると、それらを見つけることができません。
.factory('loadTemplate', function($compile) {
return function(template) {
return {
restrict: 'E',
replace: true,
templateUrl: template,
link: function(scope, el, attrs) {
console.log(el.find('.step.disabled'));
}
}
}
})
.directive('step1', function(loadTemplate) {
return loadTemplate('processes/steps/step1.html');
})
.
しかし、要素が見つからない(Chrome Consoleの出力):
context: div
length: 0
prevObject: o.fn.init[1]
selector: ".step.disabled"
__proto__: Object[0]
.
processes / step1.html:
<div>
<div toggle-box>
<div class="step" ng-class="{enabled: condition, disabled: !condition}">
...
.
そして、.disabled divがあることを確信しています。
$( '。ステップ')だけを使って要素を見つけようとしたら、クラスはすでにそこにあるので、それらを見つけることができます。
これを解決するにはどうすればいいですか?
これは平凡なものです。> http://plnkr.co/edit/AfyMQLX0WP3TRFFWRADD?P=Preview
解決
私が必要としているものは、ビューのレンダリングを終了するのを待つのを待つことで、0タイムアウトでも$ timeout関数で折り返します。このように:
プランカの例: http://plnkr.co/edit/rcqyc4aipnlq1wtpahjp?p=previewide/a>
.factory('loadTemplate', function($timeout) {
return function(template) {
return {
restrict: 'E',
replace: true,
templateUrl: template,
link: function(scope, el, attrs) {
$timeout(function() {
$('.step.disabled').attr('border-color', '#f00');
}, 0);
}
}
}
})
.directive('step1', function(loadTemplate) {
return loadTemplate('processes/steps/step1.html');
})
. 所属していません StackOverflow