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)
,但是在赋值时如何做呢?