Binding AngularJS如何绑定地图

Binding AngularJS如何绑定地图,binding,controller,scope,angularjs,Binding,Controller,Scope,Angularjs,这是我的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 typ

这是我的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>
我介绍了设置和示例


为什么测试1起作用而测试2不起作用?ng模型指令中不允许“[”吗?

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

这是一个输入错误。很抱歉。我已经更新了html。JSFIDLE示例没有,而且仍然不起作用。您的示例之所以这样做,是因为它创建了一个对象。这就是修复它的原因。为什么我必须用测试2而不是测试1创建对象。这是我当前版本的简化版本。我真正的ng模型看起来像“currentCourse.translations[locale].title”,如果可能的话,我不希望创建对象(因为我不想硬编码所有的locale)。也许这不可能。想法?也许这可以被认为是一个bug。也许你可以创建一个指令来确保某个“关键路径”存在。
确保存在=“currentCource.translations[locale]”
,如果不存在,它会将所有路径创建为对象。
angular.module('angularApp', []);

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