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

How to use underscore.js to assign an object to $rootscope?

我有这个变量保存数据

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

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

所以当我输入:

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

显示文本

The Master Tax Table has been pre populated with the current UK, ...

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

如果您想打印数组中对象的所有 "Texts" 属性,您可以像这样调用 .each() :

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

请尝试;

$scope.obj = {};

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