Is it possible to bind an object elements according to the value of a variable in angularjs -


suppose have object this:

var myobj = {     item1:{},     item2:{},     item3:{},     ... }; 

i have variable (e.g:itemholder) values item1, item2 , on.

i want bind object elements in view. there way bind according value of variable? solution below:

{{myobj.[itemholder]}} 

assuming both variables declared on scope, yes, can using javascript bracket notation so:

$scope.myobj = {     item1: {},     item2: {},     item3: {},     ... };  $scope.itemholder = 'item1'; 

then can interpolate value below:

{{ myobj[itemholder] }} // {} 

working snippet:

angular.module('myapp', [])    .controller('appcontroller', function($scope) {        $scope.myobj = {        item1: { a: 1 },        item2: { b: 2 },        item3: { c: 3 }      };        $scope.itemholder = 'item1';      });
<div ng-app="myapp" ng-controller="appcontroller">    type prop name<br>    <input type="text" ng-model="itemholder"><br>    {{ myobj[itemholder] }}  </div>  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.js"></script>


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -