json - ng-init json 对象

标签 json angularjs angularjs-ng-init

我使用 angularjs (ng-init),我想将值赋给变量作为 jsonObj。

我尝试了这个,但它不起作用。

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

还有一个问题 我想像这样赋值

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

如何解决这个问题??

谢谢

最佳答案

您可以使用窗口对象来设置您的 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
   };
}

关于json - ng-init json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25644936/

相关文章:

html - 从外部链接转到特定部分

javascript - AngularJS 表单验证不起作用

javascript - Angular 智能感知 VS2015 RTM

AngularJS选择: remove empty option and use data objects not arrays

javascript - 如何 $setdirty 到单个输入

javascript - Angular JS - 在 ng-init 中声明函数?

java - 多态 Jackson 反序列化 : getting either a string or an array of strings

javascript - 有谁知道您实际上是如何更新 safari 内容拦截器的 blockerList.json 文件的?

jquery - mvc中jquery无法获取json数据

java - 在spring中解析JSON?