【发布时间】:2013-09-23 09:43:06
【问题描述】:
我有一个从routeParam 或指令属性或其他任何东西得到的字符串,我想在此基础上在范围上创建一个变量。所以:
$scope.<the_string> = "something".
但是,如果字符串包含一个或多个点,我想将其拆分并实际“向下钻取”到范围内。所以'foo.bar' 应该变成$scope.foo.bar。这意味着简单的版本将无法工作!
// This will not work as assigning variables like this will not "drill down"
// It will assign to a variables named the exact string, dots and all.
var the_string = 'life.meaning';
$scope[the_string] = 42;
console.log($scope.life.meaning); // <-- Nope! This is undefined.
console.log($scope['life.meaning']); // <-- It is in here instead!
在读取基于字符串的变量时,你可以通过$scope.$eval(the_string)获得这种行为,但是在赋值时如何做到呢?
【问题讨论】:
标签: angularjs scope continuous-integration