访问$ scope变量,其名称存储在另一个变量中

时间:2020-08-20 14:58:00

标签: angularjs variables scope

查看代码...

<input type="text" ng-model="My.VarInScope1" />
<input type="text" ng-model="My.VarInScope2" />

和angularjs

var varname = 'My.VarInScope1';

$scope.varname = 'Hi'; ????
$scope[varname] = 'Hi'; ????

我该怎么办?

感谢和问候。

1 个答案:

答案 0 :(得分:0)

https://docs.angularjs.org/api/ng/service/$parse

$parse(varname).assign($scope, value)

(在此具体示例中,您可以执行$scope.My.VarInScope1 = 'Hi' ofc)

相关问题