gpt4 book ai didi

javascript - 我如何在angularjs指令中获得编译(?)字符串

转载 作者:行者123 更新时间:2023-11-29 22:06:05 24 4
gpt4 key购买 nike

这是我的测试指令的代码:

    mymodule.directive("test", function($compile) {
return {
restrict: 'E',
replace: true,
template:
'<div data-date="{{avail}}"></div>',
scope: {
avail: '='
},
controller: function($scope) {
$scope.dump = function(el) {
console.log($('<div>').append(el.clone()).html());
};
},
link: function postLink($scope, $element, $attrs) {

$scope.dump($element); // print '<div data-date="{{avail}}"></div>'
$scope.dump($compile($element)($scope)); // print '<div data-date="{{avail}}"></div>'

setTimeout(function() {
$scope.dump($element); // print '<div data-date="12.10.2014"></div>'
}, 1);
}
}
});

问题是:如何编译(或命名)字符串

<div data-date="12.10.2014"></div>

在 postLink 函数中(不使用 setTimeout)。

最佳答案

这很像@Ben 的回答,只是你应该取消注册 $watch 监听器

var unregister = $scope.$watch('avail', function(){
if($scope.avail) {
$scope.dump($element);
unregister();
}
});

或者你可以使用 $timeout

$timeout(function(){
$scope.dump($element);
})

关于javascript - 我如何在angularjs指令中获得编译(?)字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20928321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com