javascript - What is Angularjs $compile function 2nd and 3rd parameter -
i have example
<div ng-controller="myctrl"> <div compile-template ng-bind-html="post"></div> </div>
and angularjs code:
angular.module('myapp',[]) .controller('myctrl', function($scope, $sce,$timeout) { $scope.name = 'world'; $scope.post = $sce.trustashtml("<h1>hello {{name}}</h1>"); $timeout(function(){ $scope.name = "3333"; },3000); }); angular.module("myapp").directive('compiletemplate', ["$compile", "$parse", function($compile, $parse) { return { restrict: 'a', link: function($scope, element, attr) { var parse = $parse(attr.ngbindhtml); function value() { return (parse($scope) || '').tostring(); } $scope.$watch(value, function() { $compile(element, null,-9999)($scope); }); } } }]);
if carefully, notice function.
$compile(element, null,-9999)($scope);
if make $compile(element)($scope)
, not work longer.
why that?
here fiddle.
the third argument of $compile
maxpriority
. the docs:
only apply directives lower given priority (only effects root element(s), not children)
when run $compile(element, null,-9999)($scope);
, telling compiler skip directives on element
priority greater -9999
. why compiletemplate
directive not "self-compiled" because default priority 0 , ngbindhtml
not run twice, since:
this directive executes @ priority level 0.
when removing third param ngbindhtml
compiled , linked again. same thing happen compiletemplate
directive well. since have set $watch
and $compile
called inside of it,
[$rootscope:infdig] 10 $digest() iterations reached. aborting!
error because of infinite "self-compilation". 1 of called "double compilation" issues.
the second argument transclude
function (it not play role in case since passed null
):
function available directives - deprecated.
Comments
Post a Comment