考虑一下这个可运行的例子:为什么在el3之后没有放入el4?如果我调试并打印el3,似乎el4已经消失了。如何将元素放在另一个元素之后,以便它们成为兄弟姐妹?
编辑:在调用.after()之前,我不想追加el3。我只需要两个angular.elements,我想将它们组合成两个兄弟,并在指令元素中追加或替换它们。
angular.module('myModule', [])
.directive('mydir', [function () {
return {
restrict: 'E',
scope: {},
template:'<div><b>{{myf.str}}</b>',
link: function (scope, elem, attrs, ctrl) {
var el = angular.element('<b>First </b>');
var el2 = angular.element('<i>Second</i><br>');
el.append(el2);
elem.append(el);
var el3 = angular.element('<b>Third </b>');
var el4 = angular.element('<i>Fourth</i>');
el3.after(el4);
elem.append(el3);
}
};
}])
<html ng-app='myModule'>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<mydir></mydir>
答案 0 :(得分:0)
angular.module('myModule', [])
.directive('mydir', [
function() {
return {
restrict: 'E',
scope: {},
template: '<div><b>{{myf.str}}</b>',
link: function(scope, elem, attrs, ctrl) {
var el = angular.element('<b>First </b>');
var el2 = angular.element('<i>Second</i><br>');
el.append(el2);
elem.append(el);
var el3 = angular.element('<b>Third </b>');
var el4 = angular.element('<i>Fourth</i>');
elem.append(el3);
elem.after(el4);
}
};
}
])
<html ng-app='myModule'>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<mydir></mydir>