如何使用underscore.js将对象分配给$ rootscope?

时间:2017-06-30 13:13:27

标签: javascript angularjs json underscore.js

我有这个变量持有数据

var tooltipsJson = [{
    "Language": "en-GB",
    "Section": "Sales&Marketing",
    "ItemName": "CalculationType",
    "Texts": "Having selected the account heading select the calculation ..."
  },
  {
    "Language": "en-GB",
    "Section": "Taxes",
    "ItemName": "Save",
    "Texts": "The Master Tax Table has been pre populated with the current UK, ..."
  }
];

我想使用underscore.js 每个

_.each(.........)

所以当我把:

console.log({{tooltipsJson.Taxes.Save}});

显示文字

  

主税表已预先填写了当前的英国,......

我希望将Texts属性分配给局部变量

2 个答案:

答案 0 :(得分:1)

请尝试;

$scope.obj = {};

_.each(tooltipsJson, function( val, key ) {
    $scope.obj[key] = val;
});

答案 1 :(得分:0)

如果要打印数组中对象的所有“文本”属性,请调用.each(),如下所示:

_.each(tooltipsJson, function(item){
    console.log(item.Texts);
});