【问题标题】:Accesst to $scope variable that its name is stored in another variable访问 $scope 变量,其名称存储在另一个变量中
【发布时间】:2020-08-20 14:58:00
【问题描述】:

查看代码...

<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'; ????

我该怎么做?

感谢和问候。

【问题讨论】:

    标签: angularjs variables scope


    【解决方案1】:

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

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

    (在这个具体的例子中你可以做$scope.My.VarInScope1 = 'Hi'ofc)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 2022-01-12
      相关资源
      最近更新 更多