筛选器方法在Angular 6中返回空数组

时间:2018-08-30 19:21:49

标签: angular typescript angular6

我已经在Angular 6中创建了简单的产品组件。当用户单击某些产品类别时,它将这些产品过滤给用户。

class CollectionTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!


    override func awakeFromNib() {
        super.awakeFromNib()

        collectionView.delegate = self
        collectionView.dataSource = self

        if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        collectionViewHeightConstraint.constant = collectionView.collectionViewLayout.collectionViewContentSize.height
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "taskCell", for: indexPath as IndexPath) as! TaskCollectionViewCell

        cell.name.text = "Task_" + String(indexPath.row)

        return cell
    }
}

当我单击HTML模板中的类别时,this.product.filter方法返回空数组。我检查了过滤器条件变量(p.category === this.category),它工作正常。如果没有要匹配的类别,它应该将默认产品数组返回到filterdProduct,即使那根本不起作用。

2 个答案:

答案 0 :(得分:2)

问题在于filter方法期望返回值,而您没有返回任何东西。

有两种方法可以解决此问题,两者都可以做同样的事情。

filter(p => p.category === this.category)

filter(p => { return p.category === this.category; })

答案 1 :(得分:-1)

解决方案是删除template: ` <div> <div v-for="(item, index) in mutableOptions"> <h3>{{ index }}<h3> <h4>{{ item.Display }}<h4> </div> </div>`, created: function() { console.log(this.sentimentTypes) this.mutableOptions = this.sentimentTypes; }, 方法中的花括号,因为默认情况下,Javascript使用花括号作为代码块而不是对象。

相关问题