Ng-init json Object

I am using angularjs (ng-init) and I want to assign the value to a variable as jsonObj.

I try this one but it doesn't work.

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

and one more question I want to assign a value like

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

How to solve this problem?

thank

+3


source to share


4 answers


You can use window object to set json:

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

      

:



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

      

:



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

      

+2


source


Eliminate your quotes ...



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

      

+1


source


Try it...

    <body ng-controller="TestController">
       <div ng-init="Init()">
        {{percentObj || json }}
       </div>
    </body>

    $scope.Init = function()
    {
      $scope.percentObj = [{ "value":40,"color":"#F5A623" },{ "value":60,"color":"#F5A623"                }]
    }

      

0


source


for the second please check the code below

var obj = {};
$scope.percentObj = [];
obj.value = parseInt($scope.projectData[0].value);
obj.color = "#F5A623";
$scope.percentObj.push(obj);

      

-1


source







All Articles