gpt4 book ai didi

javascript - 为什么 templateCache 在 angular js 中不起作用?

转载 作者:行者123 更新时间:2023-11-29 15:31:24 26 4
gpt4 key购买 nike

我正在尝试使用 $templateCache。但是当我使用 $templateCache.get() 进行控制台时,它会给我未定义的 .. 为什么?

http://plnkr.co/edit/MAXQmzTVR8fpsw645rct?p=preview

<script>
angular.module('app',[ ]).directive('h',function($templateCache){
console.log($templateCache.get('about.html'))
var element=angular.element('<input type="text" ng-model="message"><div>{{message}}</div>');
return { restrict:'E',
scope:{},
templateUrl:'a.html',replace:true,
transclude:true,
compile:function(tElement){
tElement.append(element);
return function(s,e,a){
s.$watch('message',function(val){
// console.log('------'+val)
})
}
}


}
})
</script>



**console.log($templateCache.get('about.html'))** give me undefined why ?

最佳答案

因为当前您在注册该指令时正在运行该语句。那时 $templateCache 中没有模板。您需要将该行放在指令 compile 函数中,然后您才能在其中获取数据。

关于javascript - 为什么 templateCache 在 angular js 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34448238/

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