angularjs 中directive的scope问题
2 回答
狐的传说
TA贡献1804条经验 获得超3个赞
templateURL function 只有2个参数 elem 和 attr哦。不能访问scope,因为模版请求是在scope初始化之前。
可以通过属性传输,然后获取return 'customer-'+attr.type+'.html',或者
...
link: function(scope) {
scope.customerUrl = 'customer-'+scope.type+'.html'},
template: '<div ng-include="customerUrl"></div>'...
开满天机
TA贡献1786条经验 获得超13个赞
templateUrl执行还在compile之前,从哪里去注入的scope?
templateUrl的参数只有element,attrs。
如果你的type是静态的,可以这样用
<my-dir type="template.html"></my-dir>
.directive('myDir', function () { return { scope: { type: '@'
}, templateUrl: function (element, attrs) { return attrs.type;
}
}
});- 2 回答
- 0 关注
- 673 浏览
添加回答
举报
0/150
提交
取消
