ng-init json Object

user3900567 picture user3900567 · Sep 3, 2014 · Viewed 14.2k times · Source

I use angularjs (ng-init) and I want to assign value to variable as jsonObj.

I try this one but it doesn't work.

ng-init="percentObj = [{ "value":40,"color":"#F5A623" },{ "value":60,"color":"#F5A623" }];

and another question I want to assign value like

percentObj = [{ "value": parseInt($scope.projectData[0].value),"color":"#F5A623" },{ "value":  parseInt($scope.projectData[0].value),"color":"#F5A623" }]

How to fix this problem??

Thx

Answer

Nigrimmist picture Nigrimmist · May 26, 2015

You can use window object for set your json :

<script type="text/javascript">
    window.data= {awesome:1};
</script>

view :

<div ng-controller="myCntrl" ng-init="init('data')">

controller :

function myCntrl($scope) {
   $scope.init = function (settings) {
      settings = window[settings];
      console.log(settings.awesome); //1
   };
}