最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the purpose of calling $compile($element)($scope) in a directive - Stack Overflow

programmeradmin1浏览0评论

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.

Share Improve this question edited Jan 20, 2015 at 15:39 Aggieborn 3222 silver badges8 bronze badges asked Jan 15, 2015 at 23:18 liontaleliontale 911 silver badge6 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

I 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.

发布评论

评论列表(0)

  1. 暂无评论