admin管理员组文章数量:1303498
The angular code placed on jsfiddle is regarding a custom directive, that uses $pile($element)($scope)
and causes the ng-click action to happen twice:
My questions are:
- I would like to understand, why is the ng-click action happening twice?
- What is the purpose of calling
$pile($element)($scope)
? - what happens if it is not called, under what circumstances should it be called?
Here are the details and what I have gathered so far:
I would like to understand, why is the ng-click action happening twice? The following line shows the custom directive "hello" and ng-click on a button. The custom directive calls $pile($element)($scope)
and that is the line that causes the action being fired twice, but I don't understand how?
Test CLICK!
Here is the code - /
<div ng-app='myApp' ng-controller='DirectiveTestController'>
<button hello ng-click="testClick()">Test CLICK!</button>
</div>
var myApp = angular.module('myApp', []);
myApp.controller('DirectiveTestController', ['$scope',
function ($scope) {
$scope.testClick = function () {
window.alert("hey");
console.log("hey");
}
}]);
myApp.directive('hello', function () {
return {
scope: true,
controller: ['$scope', '$element', '$pile', function ($scope, $element, $pile) {
$element.removeAttr('hello');
// $element.removeAttr('ng-click');
$pile($element)($scope);
}]
};
});
What is the purpose of calling $pile($element)($scope)
, what happens if it is not called and under what circumstances should it be called?
(click on the button and you will notice that the action happens twice)
The intent of the directive is to hide/disable based on some logic.
so in this directive I see $element.removeAttr("ng-hide")
, etc, and each time the $element.removeAttr
is called it is followed with a $pile($element)($scope)
, is the purpose to re-write the DOM?
I examined the DOM and I don't see ng-click defined multiple times. When examining the DOM (firebug), I looked at $element->0->attributes->ng-click (among other elements).
If I remove ng-click using $element.removeAttr("ng-click")
then the action only happens once.
Or if I remove the $pile($element)($scope)
the action only happens once.
The angular code placed on jsfiddle is regarding a custom directive, that uses $pile($element)($scope)
and causes the ng-click action to happen twice:
My questions are:
- I would like to understand, why is the ng-click action happening twice?
- What is the purpose of calling
$pile($element)($scope)
? - what happens if it is not called, under what circumstances should it be called?
Here are the details and what I have gathered so far:
I would like to understand, why is the ng-click action happening twice? The following line shows the custom directive "hello" and ng-click on a button. The custom directive calls $pile($element)($scope)
and that is the line that causes the action being fired twice, but I don't understand how?
Test CLICK!
Here is the code - http://jsfiddle/4x4L3gtw/27/
<div ng-app='myApp' ng-controller='DirectiveTestController'>
<button hello ng-click="testClick()">Test CLICK!</button>
</div>
var myApp = angular.module('myApp', []);
myApp.controller('DirectiveTestController', ['$scope',
function ($scope) {
$scope.testClick = function () {
window.alert("hey");
console.log("hey");
}
}]);
myApp.directive('hello', function () {
return {
scope: true,
controller: ['$scope', '$element', '$pile', function ($scope, $element, $pile) {
$element.removeAttr('hello');
// $element.removeAttr('ng-click');
$pile($element)($scope);
}]
};
});
What is the purpose of calling $pile($element)($scope)
, what happens if it is not called and under what circumstances should it be called?
(click on the button and you will notice that the action happens twice)
The intent of the directive is to hide/disable based on some logic.
so in this directive I see $element.removeAttr("ng-hide")
, etc, and each time the $element.removeAttr
is called it is followed with a $pile($element)($scope)
, is the purpose to re-write the DOM?
I examined the DOM and I don't see ng-click defined multiple times. When examining the DOM (firebug), I looked at $element->0->attributes->ng-click (among other elements).
If I remove ng-click using $element.removeAttr("ng-click")
then the action only happens once.
Or if I remove the $pile($element)($scope)
the action only happens once.
3 Answers
Reset to default 3I think the main reason why this is happening is because you are using a click event in the element where you apply the directive, instead of defining this event directly in the directive. Therefore you are getting the click from the button element, but also the click from the directive controller.
What $piles do is once invoked against markup will produce a function you can use to bind the markup against a particular scope (what Angular calls a linking function), that's why Rodion suggested using link, I guess. In this particular case, it means that you are are using an event directly in the element button but then linking it again to the scope in your directive by using $pile. I guess that's why you get the message twice.
As I don't know whether this is clear, I obtained the information from this link http://odetocode./blogs/scott/archive/2014/05/07/using-pile-in-angular.aspx where it is way better explained.
Here also a JSFiddle where you can see how it works (excerpt from the article above).
app.directive("otcDynamic", function($pile){
return {
link: function(scope, element){
var template = "<button ng-click='doSomething()'>{{label}}</button>";
var content = $pile(template)(scope);
element.append(content);
}
};
});
JSFiddle
I think such operations (with html) should be in link (not in controller):
link: function (scope, element) {
element.removeAttr('hello');
}
http://jsfiddle/4x4L3gtw/32/
pile - $pileProvider - service in module ng Compiles an HTML string or DOM into a template and produces a template function, which can then be used to link scope and the template together.
The pilation is a process of walking the DOM tree and matching DOM elements to directives.
本文标签: javascriptWhat is the purpose of calling compile(element)(scope) in a directiveStack Overflow
版权声明:本文标题:javascript - What is the purpose of calling $compile($element)($scope) in a directive - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741759141a2396295.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论