猿问

angularjs 中directive的scope问题?

幕布斯6054654
浏览 620回答 2
2回答

狐的传说

templateURL function 只有2个参数&nbsp;elem&nbsp;和&nbsp;attr哦。不能访问scope,因为模版请求是在scope初始化之前。可以通过属性传输,然后获取return 'customer-'+attr.type+'.html',或者... link:&nbsp;function(scope)&nbsp;{ &nbsp;&nbsp;scope.customerUrl&nbsp;=&nbsp;'customer-'+scope.type+'.html'}, template:&nbsp;'<div&nbsp;ng-include="customerUrl"></div>'...

开满天机

templateUrl执行还在compile之前,从哪里去注入的scope?templateUrl的参数只有element,attrs。如果你的type是静态的,可以这样用<my-dir&nbsp;type="template.html"></my-dir>.directive('myDir',&nbsp;function&nbsp;()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scope:&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type:&nbsp;'@' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;templateUrl:&nbsp;function&nbsp;(element,&nbsp;attrs)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;attrs.type; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} });
随时随地看视频慕课网APP

相关分类

AngularJS
我要回答