首页 > angularjs 中directive的scope问题

angularjs 中directive的scope问题

重新编辑

各位请看:link中我可以输出scope.type
可是在templateURL中scope.type为undefined
有点不懂directive的scope了,请各位指教谢谢


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;
        }
    }
});

你有什么问题?


把url的函数去掉,直接把地址拼出来试试!?


templateURL function 只有2个参数 elemattr哦。不能访问scope,因为模版请求是在scope初始化之前。

可以通过属性传输,然后获取return 'customer-'+attr.type+'.html',或者

...
link: function(scope) {
  scope.customerUrl = 'customer-'+scope.type+'.html'
},
template: '<div ng-include="customerUrl"></div>'
...
【热门文章】
【热门文章】