I'm trying to filter an an array in the dom that uses ng-repeat. The filter works when I hard code the return, but when I try to use this
, it gives me the error
Cannot read property minHeight of undefined.
So for some reason this is undefined, but it works in other functions in the controller. I'm not sure why it doesn't work in the filter.
I'm sure I'm missing something ridiculously simple, but I cannot find it
Controller
export class ItemComponent {
/*@ngInject*/
constructor($http) {
this.$http = $http;
this.minHeight = 0;
}
filter(row){
console.log(this.minHeight);
return row.minHeight > this.minHeight;
}
HTML
ng-repeat="item in itemCtrl.items | filter: itemCtrl.filter"
Transpiled Code (from app.bundle.js in browser)
var ItemsComponent = exports.ItemsComponent = function () {
/*@ngInject*/
ItemsComponent.$inject = ['$http'];
function ItemsComponent($http) {
_classCallCheck(this, ItemsComponent);
this.$http = $http;
this.minHeight = 0;
}
_createClass(ItemsComponent, [{
key: '$onInit',
value: function $onInit() {
this.loadItems();
}
}, {
key: 'loaditems',
value: function loadItems() {
var _this = this;
this.$http.get('/api/items').then(function (res) {
_this.items = res.data;
console.log(_this.items);
}).catch(function (err) {
console.log(err);
});
}
}, {
key: 'filter',
value: function filter(row) {
console.log(this.minHeight);
return row.minHeight > 3;
}
}]);
return ItemsComponent;
}();