json - 创建动态范围变量

标签 json angularjs angularjs-scope

我正在尝试创建动态范围变量。

这是 json 对象:

{
"status": true,
"res": {
    "call": [
        {
            "percent": 100,
            "name": "150+"
        }
    ],
    "stores": [
        {
            "percent": 100,
            "name": "150+"
        }
    ],
    "web": [
        {
            "percent": 21,
            "name": "120-150"
        },
        {
            "percent": 79,            
            "name": "150+"
        }
    ]
  }
}

这是我想要做的代码:

for(keys in jsonObject.res){
    $scope.<SomeString+keys> = 'something';
}

请建议我实现此目标的更好方法是什么?

假设 jsonObject 是我上面指定的对象

最佳答案

$parse将 Angular 表达式转换为带有 .

的函数
for(key in jsonObject.res){
    $parse('someObj.key').assign($scope, 'somevalue')
    $scope.apply()
}

关于json - 创建动态范围变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19786229/

相关文章:

json - 从具有未知结构 SWIFT 的 JSON 中查找值

javascript - 无法通过单击按钮向上滚动到菜单顶部 - AngularJS

javascript - AngularJS - 不尊重 Z-Index 更改上下滑动 Div

angularjs - 没有模板的 Angular 组件表达式绑定(bind)

python - 在 pynacl 中将公钥作为 JSON 对象发送

json - 尝试从 json 获取数据时对成员 `jsonObject(with:options:)` 的引用不明确

javascript - 过滤日期在 AngularJS 中返回 NaN-NaN-NaN

javascript - AngularJS 服务/工厂从 $scope 变量动态 $compile HTML

php - 在 Laravel 中找不到类 'App\Http\Controllers\Response' 错误

html - 如何解析 Div 类中的 Angular 作用域变量