【问题标题】:Angular Directive incoming scope value is undefined角度指令传入范围值未定义
【发布时间】:2017-04-12 06:43:13
【问题描述】:

在 html 中,我将此对象传递给指令

<lcd-code ldcCode="{{ detail.program.ldcCode }}"></lcd-code>

detail.program.ldcCode = "PSIH" ...

然而在指令中它是未定义的

var lcdCode = function (customerService, $sce) {
    return {
        replace: true,
        restrict: "E",
        scope: {
            ldcCode: "="  // two way 
        },
         link: function (scope, element, attrs) {

            console.log('scope.ldcCode',scope.ldcCode);   // says undefined

         }
    };
}

以前我使用“@”,然后 attrs.ldcCode 似乎可以工作......我猜我正在处理的数据的最终结果并发回我想我想要 2 路数据绑定。

【问题讨论】:

    标签: javascript angularjs angularjs-scope angular-directive


    【解决方案1】:

    你需要不使用{{}}进行双向绑定

    <lcd-code ldcCode="detail.program.ldcCode"></lcd-code> 
    

    【讨论】:

    • 还没有定义
    • &lt;lcd-code ldcCode="detail.program.ldcCode"&gt;&lt;/lcd-code&gt;
    • console.log('scope.ldcCode',scope.ldcCode); 说未定义
    • @JohnBaxter 你的detail.program.ldcCode 有什么价值吗?
    • 是的,它可以将其吐出到页面 {{ }} 并显示 PSIH
    【解决方案2】:

    您不必使用 Expression ,删除 {{}}

    <lcd-code ldcCode="detail.program.ldcCode"></lcd-code>
    

    【讨论】:

    • 没有表达式仍然是未定义的
    猜你喜欢
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 2016-01-20
    • 2016-07-20
    • 1970-01-01
    • 2013-10-04
    相关资源
    最近更新 更多