gpt4 book ai didi

javascript - 如何从 AngularJS 指令中选择动态生成的元素?

转载 作者:可可西里 更新时间:2023-11-01 14:47:41 24 4
gpt4 key购买 nike

在我的指令中,我需要选择某些 DOM 元素,其中一些元素是在 ng-repeat 循环中动态生成的。如果我以直接的方式进行,我只会得到静态元素。但是,如果我将选择延迟 500 毫秒,我将获得所有元素,这正是我想要的。

虽然这可行,但它不是一个理想的解决方案,而且看起来肯定不是最佳实践。一方面,您希望超时时间尽可能短,但另一方面,您希望在选择之前确保 DOM 已准备就绪。

当所有动态 DOM 准备就绪时,是否会触发一个事件?从 AngularJS 指令中选择动态生成的元素的推荐方法是什么?

示例:

HTML:

<div data-my-directive>
<div class="modal-body">
<label data-localize>type:</label>&nbsp;
<select class="form-control" ng-model="assetFilter.appCode" ng-change="loadassets(assetFilter.appCode)" ng-options="type.code as type.name for type in types"></select>

<table class="table table-default" ng-show="hasLoaded">
<tbody ng-repeat="asset in assets | filter:assetFilter | orderBy:'assetKey':false">
<tr>
<td>
<div class="container-fluid">
<div class="row vert-align">
<div class="col-sm-4">
{{asset.assetKey}}
</div>
<div class="col-sm-8" style="height:100%">
<input ng-hide="asset.assetKey.length >= 80" type="text" class="form-control" ng-model="asset.assetValue" ng-change="asset.isModified=true">
<textarea ng-show="asset.assetKey.length > 80" class="form-control" ng-model="asset.assetValue" ng-change="asset.isModified=true"></textarea>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>

</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="save(saveassets, $event)" ng-disabled="!(assets | anyModified)" data-localize>Save</button>
<button class="btn btn-warning" ng-click="close($event)" data-localize>Close</button>
</div>
</div>

指令:

myApp.directive('myDirective', function ($timeout) {
return {
restrict: 'A', //attribute only
link: function (scope, elem, attr, ctrl) {
var context = elem[0];
var availableFormElements = 'input:not([disabled]):not([class*=ng-hide]),' +
'select:not([disabled]):not([class*=ng-hide]), textarea:not([disabled]):not([class*=ng-hide]),' +
'button:not([disabled]):not([class*=ng-hide]),' +
'*[class*=btn]:not([disabled]):not([class*=ng-hide])';

var allFormElements = context.querySelectorAll(availableFormElements);
// Will only get static elements, nothing from ng-repeat loop

$timeout(function () {
allFormElements = context.querySelectorAll(availableFormElements);
// Will include all elements, also from ng-repeat loop
}, 500);

// Code to manipulate selected form elements

};
});

最佳答案

这是一个简单的示例,您可以如何计算它。Imo 这个解决方案的唯一缺点是你不能使用隔离范围。

html

<div data-ng-controller="MainController">
<div outer-directive>
<ul>
<li ng-repeat="asset in assets" inner-directive>
{{asset}}
<input type="text" class="form-control">
</li>
</ul>
</div>
</div>

js

var app = angular.module('myApp', []);

app.controller('MainController',function($scope) {
$scope.assets = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
});

app.directive('outerDirective', function() {
return {
restrict: 'A',
controller: function($scope) {

}
};
});
app.directive('innerDirective', function() {
return {
restrict: 'A',
require: '^outerDirective',
link: function(scope, elem, attrs,ctrl) {
var context = elem[0];
if (scope.$last){
var availableFormElements = 'input,textarea';
var allFormElements = context.querySelectorAll(availableFormElements);
console.log(allFormElements);
}
}
};
});

或更好

.directive('myParent', function ($timeout) {
return {
restrict: 'A', //attribute only
controller: function ($scope, $element) {
this.isDone = function(){
var context = $element[0];
var availableFormElements = 'input,textarea';
var allFormElements = context.querySelectorAll(availableFormElements);
console.log(allFormElements);
}
}
};
})
.directive('myChild', function ($timeout) {
return {
require:'^myParent',
restrict: 'A', //attribute only
link: function (scope, elem, attr, ctrl) {

if (scope.$last){
ctrl.isDone();
}
}
};
})

顺便说一下不要触摸 Controller 中的 dom :)

关于javascript - 如何从 AngularJS 指令中选择动态生成的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26691122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com