As mentioned, I have written two instructions. I want to apply them to the same element. First perform repeat and then bind. However, currently, only the first one executes bind
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>angular</title>
<script src="http://cdn.bootcss.com/angular.js/1.2.10/angular.min.js"></script>
</head>
<body ng-app="app">
<p ng-controller="test">
<input type="text" ng-model="msg">
<p lxc-bind="msg" lxc-reapeat="3">123123</p>
<!-- <p lxc-reapeat="3">lxc</p> -->
</p>
<script>
angular.module('app',[])
.controller('test',function($scope){
$scope.msg = 'test';
})
.directive('lxcBind',function(){
// Runs during compile
return {
restrict: 'A', // E = Element, A = Attribute, C = Class, M = Comment
link: function($scope, iElm, iAttrs, controller) {
$scope.$watch(iAttrs.lxcBind,function(newValue){
iElm.text(newValue);
})
}
};
})
.directive('lxcReapeat',function(){
return {
restrict:'A',
priority: 1000,
link:function($scope,$elm,$attr){
var repeatNum = $attr.lxcReapeat;
for (var i=repeatNum-2;i>= 0;i--){
var dom = $elm.clone();
$elm.after(dom);
}
}
}
})
</script>
</body>
</html>
First point, the
clone
method only clones nodes, and bound events will not be cloned.Second point, when adding nodes to a document dynamically, the angular directive will not take effect and requires dynamic compilation.
Rendering:
and
ng-repeat
The above implementation has no functional problems, but the formation is not very uniform. So, I made a slight modification
Final comparison
请采纳
.