【问题标题】:Is it possible to call a function string in ng-click?是否可以在 ng-click 中调用函数字符串?
【发布时间】:2013-12-18 19:33:28
【问题描述】:

我将函数作为字符串从 json 返回。

JSON 类似于以下内容。

menu:[
  {action:"setItem()", p1:"{{this}}", text:"Add Item"},
  {action:"removeItem()", p1:"{{this}}", text:"Remove Item"}
]

<div ng-repeat="menuList in menu">
  <a href="#" ng-click="menu.action">{{menu.text}}</a>
</div>

如何通过 ng click 调用函数,即在单击 Add Item 时调用 setItem()。

【问题讨论】:

    标签: javascript jquery json angularjs


    【解决方案1】:

    不太确定你能做到你所要求的。如果你可以把json转成这个:

    menu:[
      {action: setItem, p1:"{{this}}", text:"Add Item"},
      {action: removeItem, p1:"{{this}}", text:"Remove Item"}
    ]
    

    ng-click 到 ng-click="menu.action()" 就可以了

    【讨论】:

      【解决方案2】:

      我认为这应该可行:

      <a href="#" ng-click="{{menu.action}}">{{menu.text}}</a>
      

      【讨论】:

        猜你喜欢
        • 2017-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多