Angularjs-使用指令添加ng-*属性
我正在尝试向由指令管理的元素添加简单的ng mouseover绑定。但是colud没有让它工作 @ 我尝试在添加ng mouseover绑定后重新编译元素。directive.compile和外部控制器运行,但directive.linker不运行 @ 我已经把$compile'ing移到了linker中。它运行正常,ng mouseover运行正常,但在链接器中重新编译会导致无休止的循环,最终导致浏览器崩溃:)Angularjs-使用指令添加ng-*属性,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我正在尝试向由指令管理的元素添加简单的ng mouseover绑定。但是colud没有让它工作 @ 我尝试在添加ng mouseover绑定后重新编译元素。directive.compile和外部控制器运行,但directive.linker不运行 @ 我已经把$compile'ing移到了linker中。它运行正常,ng mouseover运行正常,但在链接器中重新编译会导致无休止的循环,最终导致浏览器崩溃:) 如何使用指令向元素添加ng-*绑定?在这些方法中我做错了什么?我以为你和我在同一条
如何使用指令向元素添加ng-*绑定?在这些方法中我做错了什么?我以为你和我在同一条船上,但也许你不是。无论如何,这里有两种解决方案。我被卡在第二个 1) 特定元素的指令 如果您知道要处理的元素将是div、span、h1等等,或者这并不重要(取一个元素并替换为您需要的元素) HTML HTML/输出
同上。将HTML中的
div
元素更改为nav
,输出将反映更改。您是否直接尝试向元素添加绑定而不是属性:element.bind('mouseover',function(e){bager()})代码>?我对你的代码做了一点修改,检查这里:(我在控制台中写道,警报让我发疯)@dotdot为警报感到抱歉:)我一直在尝试使用angular,以便更好地学习,而没有使用jquery goodies或类似element.bind的事件绑定。我做错的是用directive标记重新编译元素,这导致了无休止的循环。在可编辑div中插入一个子项并编译它而不是父项解决了我的问题。谢谢。如果能看到最后的代码就太好了…@Patrick我不记得确切的代码了,但它是这样的
<div data-mydirective>
<span>some other stuff</span>
<div>more stuff</div>
</div>
module.directive( 'mydirective', [ function() {
return {
restrict: "A",
controller: function( $scope ) {
$scope.test = function() {
console.log('howdy');
}
},
template: "<div data-ng-transclude data-ng-mouseover='test()'></div>",
transclude: true,
replace: true,
link: function ( scope, element, attrs ) {
}
};
}]);
<div ng-mouseover="test()" data-ng-transclude="" data-mydirective="">
<span class="ng-scope">some other stuff</span>
<div class="ng-scope">more stuff</div>
</div>
module.directive( 'mydirective', [ function() {
return {
restrict: "A",
controller: function( $scope ) {
$scope.test = function() {
console.log('howdy');
}
},
template: function( element, attrs ) {
var tag = element[0].nodeName;
return "<"+tag+" data-ng-transclude data-ng-mouseover='test()></"+tag+">";
},
transclude: true,
replace: true,
link: function ( scope, element, attrs ) {
}
}
}]);