【问题标题】:Concat string in $scope angularjs [duplicate]$scope angularjs中的Concat字符串[重复]
【发布时间】:2017-01-21 03:17:55
【问题描述】:

我想通过函数中的参数和 $scope 中的 concat 传递字符串名称,这可能吗?

function test(txt){
 $scope.person. + txt; // I want to do $scope.person.status; for example
}

谢谢。

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    是的,只要这样做:

    function test(txt){
       $scope.person[txt];
    }
    

    【讨论】:

    • 谢谢,这可行 =]
    【解决方案2】:
    function test(txt){
      $scope.person[txt] = '';  // We need to assign something to it if not it wont work;
     }
    

    演示示例:https://jsbin.com/gihizo/3/edit?html,js,console,output

    【讨论】:

      【解决方案3】:

      检查Property accessors

      作为任何对象,您可以使用点表示法括号表示法访问它们的属性。当您事先不知道要访问的属性时,最好使用括号表示法

      【讨论】:

      • 谢谢,我去看看。
      猜你喜欢
      • 2019-01-08
      • 2013-04-13
      • 2012-01-22
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 2015-11-28
      • 2014-12-31
      相关资源
      最近更新 更多