angularjs中大多在指令之中操作DOM,现在遇到一个指令中无法获取DOM的特殊情况:
var directiveCtrl=angular.module("directiveCtrl",[]); directiveCtrl.controller("ctrlTwo",['$scope',function($scope){ $scope.name="me"; }]); directiveCtrl.directive("num",function(){ return{ restrict: 'E', template: '<div ng-repeat="i in [1,2]" ng-controller="ctrlTwo">`name`</div>', replace:true, link:function(scope,ele,attr){ console.log(ele); } }; });
输出的是[comment]而不是[div.ng-scope.ng-binding],这样无法进行DOM元素的操作。
我的解决方法是:把
ng-repeat="i in [1,2]"
放在num标签的外层标签上,一切就正常了,当然这只是避开了问题。如果有一天知道了问题所在,我会再跟上一篇作为补充。
转载于:https://blog.51cto.com/11426117/1760873