【问题标题】:AngularJS - Check if component's bindings function is a functionAngularJS - 检查组件的绑定函数是否是一个函数
【发布时间】:2019-02-09 06:20:24
【问题描述】:

我正在尝试检查我的组件是否绑定。 如您所知,要绑定函数,您必须使用 '&' 字符。

<pipeline-list-item
    item="item"
    is-selected="item === $ctrl.selectedItem"
    on-action="">
</pipeline-list-item>

.component('pipelineListItem', {
    restrict: 'E',
    bindings: {
        onAction: '&'
    },
    template: require('./pipeline-list-item.html'),

    controller($scope, RESULTS_TABS, SESSION_ACTIONS, SESSION_STATUSES, ACTIONS, Logger) {
        var that = this;

        that.actionClicked = function ($event, action) {
            $event.stopPropagation();
            $event.preventDefault();
            if (typeof that.onAction === 'function') {
                console.log(that.onAction);
                that.onAction({item: that.item, action});
            }
            else {
                Logger.error({
                    method_name: 'pipelineListItem.actionClicked.',
                    message: 'onAction function was not given to component.'
                });
            }
        };
    }

因为 '&' char AngularJS 正在用另一个函数包装函数,所以 typeof === 'function' 返回 true。

The function that I got.

谢谢,伊泰。

【问题讨论】:

    标签: angularjs function binding components


    【解决方案1】:

    您必须附加一个“?”到你的绑定,所以 '&?'就是答案。

    现在您可以使用“if(that.onAction) {that.onAction()}”来检查它。

    玩得开心!

    如果你不附加'?',你仍然可以这样做。onAction()。如果传递了一个函数,它将调用它。如果没有传递任何内容,则会调用一个空函数。

    【讨论】:

      猜你喜欢
      • 2020-03-21
      • 2016-09-13
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多