【问题标题】:AngularJS: Why doesn't the directive see an object on the scopeAngularJS:为什么指令看不到范围内的对象
【发布时间】:2014-06-15 15:22:02
【问题描述】:

我正在尝试使用 Angular。为了这个问题,我已经更改了this fiddle

当我尝试从指令中访问范围变量“password”和“password_confirmation”时,它会很好地显示它们。见this fiddle。像这样:

scope.$apply(function(){
          scope.passwordAsSeenByDirective = scope['password'] + "...";
          scope.password_confirmationAsSeenByDirective = scope['password_confirmation'] + "...";
        });

但是当我创建一个对象并将它们作为属性放在那里时,我就无法再访问它们了。见this fiddle。像这样:

scope.$apply(function(){
          scope.passwordAsSeenByDirective = scope['obj.password'] + "...";
          scope.password_confirmationAsSeenByDirective = scope['obj.password_confirmation'] + "...";
        });

怎么会?

【问题讨论】:

    标签: angularjs angular-directive


    【解决方案1】:

    试试这个scope['obj']['password']

    【讨论】:

    • 非常感谢你们!
    【解决方案2】:

    我不能给你完整的答案,因为我自己还在学习 AngularJS。但是,我可以提供的一点帮助是,您无法访问范围 [“foo.bar”] 之类的变量。 "foo.bar" 是一个 Angular 表达式,而不是属性语法,因此必须使用 $parse 解析或使用 scope.$eval

    进行评估

    【讨论】:

    • 不客气。愿意分享一个工作小提琴,以便我们都可以了解更多信息?
    • 只有当你想硬编码时,Hawk 的答案才是好的。了解这里发生的事情很重要。 Angular 符号可以扩展为标准符号。您可以像 hawk 建议的那样手动完成,但最好使用我提到的适当工具动态完成。
    猜你喜欢
    • 2012-12-12
    • 1970-01-01
    • 2015-01-12
    • 2015-10-09
    • 2023-03-21
    • 2014-03-28
    • 2016-03-06
    • 2014-11-24
    • 1970-01-01
    相关资源
    最近更新 更多