javascript - 执行从 AngularJS 中的变量调用的函数

标签 javascript angularjs

我的代码:

<li class="grid--col2of10" ng-repeat="button in buttons">
   <button ng-show="button.show" class="btn--action-{{button.color}} icon-{{button.icon}}" ng-click="{button.clickAction}">{{button.text}}</button>
</li>

我的目标:

var btns = {
    "back": {
      "color": "red",
      "clickAction": "prevStep",
      "icon": "left",
      "text": "Back"
    }, 
    "cancel": {
      "color": "red",
      "icon": "block",
      "text": "Cancel"
    },
    "clear": {
      "color": "blue",
      "icon": "cancel",
      "text": "Clear"
    },
    "save": {
      "color": "green",
      "icon": "download",
      "text": "Save"
    },
    "next": {
      "color": "green",
      "clickAction": "nextStep",
      "icon": "right-after",
      "text": "Save and Continue"
    }
  };

是否可以使 button.clickAction 成为在 AngularJS 中执行的函数?

btns 对象位于指令内部,并且使用指令 templateUrl 选项调用 html。

最佳答案

想通了。我无法在 HTML 中执行它,但我通过调用指令对象中的函数使其工作:

var btns = {
    "back": {
      "color": "red",
      "clickAction": function() { prevStep(); },
      "icon": "left",
      "text": "Back"
    }, 
    "cancel": {
      "color": "red",
      "icon": "block",
      "text": "Cancel"
    },
    "clear": {
      "color": "blue",
      "icon": "cancel",
      "text": "Clear"
    },
    "save": {
      "color": "green",
      "icon": "download",
      "text": "Save"
    },
    "next": {
      "color": "green",
      "clickAction": function() { nextStep(); },
      "icon": "right-after",
      "text": "Save and Continue"
    }
  };

和 HTML:

<li class="grid--col2of10" ng-repeat="button in buttons">
   <button ng-show="button.show" class="btn--action-{{button.color}} icon-{{button.icon}}" ng-click="button.clickAction()">{{button.text}}</button>
</li>

关于javascript - 执行从 AngularJS 中的变量调用的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15880711/

相关文章:

javascript - AngularJS:使用特定键以模式更新 JSON

javascript - React 的 useRef 钩子(Hook)不接受函数?

javascript - jQuery 插件 : If I want to call something like $ ('selector' ). plugin.group.method(),我该如何实现?

html - AngularJS-如何使用 ng-hide 隐藏元素?

css - angular.js 应用程序中的样式

javascript - IndexedDB 的包装函数

javascript - 如何使用 JQuery 滚动到 ng-repeat Angular JS 生成的 div 的底部?

angularjs - 如何在 angular.js 中使用嵌套的 ng-repeat 动态打印表行中的数组对象?

javascript - Angularjs Controller 打破了我的表达

javascript - 生态错误消息 : Unexpected 'INDENT' in Windows