Angular binding to square bracket notation property with a space

穿精又带淫゛_ 提交于 2019-12-23 01:28:17

问题


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.

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']']".

Edit: Figured out the solution. Using single quotes on the outside and double quotes on the inside:

<input ng-mode='myObject["my property"]' />

回答1:


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>


来源:https://stackoverflow.com/questions/26865892/angular-binding-to-square-bracket-notation-property-with-a-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!