AngularJs Component和angularjs-dropdown-multiselect

时间:2017-06-16 07:14:08

标签: angularjs angular-components ng-dropdown-multiselect

我是AngularJs世界的新手,并试图在组件内部使用angularjs-dropdown-multiselect。

组件的HTML看起来像:

<div>
  <select id="statusSelection" class="form-control"
          ng-options="status.name for status in $ctrl.statuses track by status.id"
          ng-model="$ctrl.status" ng-change="$ctrl.filterChanged()"></select>
</div>
<div ng-dropdown-multiselect="" options="$ctrl.categories" selected-model="$ctrl.category"
     events="$ctrl.events">
</div>

事件状态已更改,类别将调用相同的操作。

MultiselectController.prototype.filterChanged = function() {
        this.onFilterChange({
          status: this.status,
          category: this.category});
    };
MultiselectController.prototype.events = {
        onItemSelect: function(item) {
            filterChanged();
        },
        onItemDeselect: function(item) {
            filterChanged();
        }
    };

当我尝试运行上面的代码并更改状态时,代码按预期工作,但在类别更改期间失败(控制台中的错误)。

错误消息:ReferenceError:未定义filterChanged

角度版:1.5.8

angularjs-dropdown-multiselect :1.11.8

Plunker http://plnkr.co/edit/JL7N6M?p=preview

感谢您帮助我。

1 个答案:

答案 0 :(得分:0)

我创建了一个实例变量并将其初始化为Controller的实例。

var _instance;
function MultiselectController($scope) {
    this.statuses = testMultiselect.statuses;
    this.categories = testMultiselect.categories;
    this.$scope = $scope;
    this.setDefault();
    _instance = this;
}

现在,我正在使用此实例变量来访问Controller上的函数。

MultiselectController.prototype.events = {
        onItemSelect: function(item) {
            _instance.filterChanged();
        },
        onItemDeselect: function(item) {
            _instance.filterChanged();
        }
    };

我对此并不完全满意,因为应该有更好的方法来做同样的事情,但在找到之前,我会保留这个。

更新了Plunker:http://plnkr.co/edit/D7BKI9?p=preview