AngularJS: bind to all properties of an object?

I have a model that accepts an object that can contain any set of properties.

I am wondering if I can bind to each object without knowing the value of the properties ahead of time.

Take this for example

var object1 = { Name: '1', Identifier: '12345', Password: 'password' } var object2 = { Name: '2', Identifier: 'object_two' } var objects = [object1,object2]; 

My objects can have different unique properties, and I would like to bind them to a form.

I know that I can use for (the var property in the object) and iterate over each object. But can I do the same with AngularJS binding?

I wanted to do something like:

 <ul><li ng-repeat="attrib in obj"> {{attrib}} </li</ul> 

Edit: thanks to the answer below, I got a bit further. It seems I can not achieve two-way binding. This fiddle illustrates what I'm trying to do:

http://jsfiddle.net/EpqMc/17/

I essentially want to bind using something like this:

 <p ng-repeat="(key,value) in obj"> {{key}} : <input ng-model="obj[key]" /> </p> 

This works, except that I can only enter one character at a time - interesting.

Final Edit: I found an acceptable alternative if anyone else has this problem. I was not able to directly bind to the object without any problems, but I was able to associate it with an array of document status type in the answer below.

The following script does what I need to do, but only a little more verbose.

http://jsfiddle.net/8ENnx/9/

 function ctrl($scope) { $scope.objects = [ [ {Name: 'Name:', Value: 'Joe'}, {Name: 'Identification', Value: '1'}, {Name: 'Password', Value: 'secret'} ], [ {Name: 'Name:', Value: 'Jane'}, {Name: 'Identification', Value: '2'}, {Name: 'Weather', Value: 'Sunny'} ] ]; // $scope.setSelected = ?????; } <div ng-app> <div ng-controller="ctrl"> Objects <ul> <br/> Listing of properties: <br/> <li ng-repeat="obj in objects" class="swatch"> {{obj}} <p ng-repeat="prop in obj"> {{prop.Name}}: <input ng-model="prop.Value" /></p> </li> </ul> </div> </div> 

This allows me to define an arbitrary set of arguments, but still bind without problems using angular.

+6
source share
1 answer

No problems:

 <li ng-repeat='(key, value) in obj'> {{key}} : {{value}} </li> 

The docs also: http://docs.angularjs.org/api/ng.directive:ngRepeat

+10
source

Source: https://habr.com/ru/post/954628/


All Articles