角绑定到带空格的方括号符号属性 [英] Angular binding to square bracket notation property with a space
问题描述
是否可以使用访问属性的方括号符号将对象绑定到成角度的属性中.例如(使用伪代码).
Is it possible to bind to a property in angular using the square bracket notation of accessing properties. For example (using pseudo-code).
<script type="text/javascript">
var object = { };
object["my property"] = 3;
</script>
<input ng-model="object['my property']" />
我知道支持方括号表示法,但是似乎没有一种方法可以绑定到其中带有空格的属性.
I know square bracket notation is supported, but there doesn't seem to be a way to bind to a property that has a space in it.
虽然这只是伪代码,但我们在角度收到的特定错误是:未捕获的错误:语法错误,无法识别的表达式:select [ng-model ='ticket.Properties ['assigned to']']".
While this is just pseudo code, the specific error we receive in angular is : "Uncaught Error: Syntax error, unrecognized expression: select[ng-model='ticket.Properties['assigned to']']".
找出解决方案.在外部使用单引号,在内部使用双引号:
Figured out the solution. Using single quotes on the outside and double quotes on the inside:
<input ng-mode='myObject["my property"]' />
推荐答案
这是绝对可能的,请参见我在此处创建的代码段.
This is absolutely possible, see the snippet here I created.
我刚刚将初始化代码移到了控制器中.
I just moved the initialization code into a controller.
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.object = {};
$scope.object['my property'] = 3;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="myCtrl">
<input ng-model="object['my property']" />
<pre>{{object|json}}</pre>
</div>
</div>
这篇关于角绑定到带空格的方括号符号属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!