javascript - angularjs 中directive的scope问题
大家讲道理
大家讲道理 2017-04-11 11:23:14
[JavaScript讨论组]

重新编辑

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

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(4)
天蓬老师

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

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

...
link: function(scope) {
  scope.customerUrl = 'customer-'+scope.type+'.html'
},
template: '<p ng-include="customerUrl"></p>'
...
伊谢尔伦

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

你有什么问题?

天蓬老师

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号