【问题标题】:AngularJS how to bind mapsAngularJS如何绑定地图
【发布时间】:2012-08-10 08:10:39
【问题描述】:

这是我的 HTML:

<div ng-app="angularApp">
    <div ng-controller="testCtrl">
        testKey = {{testKey}}<br />
        Test 1: <input type="text" ng-model="test.myKey" />{{test[testKey]}}<br />

        Test 2: <input type="text" ng-model="test[testKey]" />{{test[testKey]}}
    </div>
</div>

这里是js:

angular.module('angularApp', []);

function testCtrl($scope)
{
    $scope.testKey = "myKey";
}​

我设置和示例here

为什么测试 1 有效但测试 2 无效? ng-model 指令中是否不允许使用“[”?

【问题讨论】:

    标签: binding controller scope angularjs


    【解决方案1】:

    Here 一个工作示例。问题很简单,test.[testKey] 无效,你要test[testKey]。并且您需要将test 定义为控制器上的对象,因为您无法设置未定义变量的属性。

    【讨论】:

    • 那是一个错字。对于那个很抱歉。我已经更新了html。 jsFiddle 示例没有,它仍然无法正常工作。您的示例确实如此,因为它创建了一个对象。这就是修复它的原因。为什么我必须使用测试 2 而不是测试 1 创建对象。这是我当前问题的简化版本。我真正的 ng-model 看起来像“currentCourse.translations[locale].title”,如果可能的话,我宁愿不必创建对象(因为我不想硬编码所有语言环境)。也许这是不可能的。想法?
    • 也许这可以被认为是一个错误。也许您可以创建一个指令来确保存在某个“关键路径”。 ensure-existence="currentCource.translations[locale]",如果不存在,则将所有路径创建为对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    相关资源
    最近更新 更多