AngularJS组件绑定功能

比方说,我们正在尝试使用网络api从列表中删除一个项目。我们使用参数-item和onRemove创建了一个名为remove-item的子组件。单击某个项目时,我们想触发父代码的回调函数。根据代码,删除该项目后不会调用onRemove。有人可以帮助我们找出错误并为我们提供插图的正确解决方案。

remove.component.js
-------------------
this.RemoveItem = function (index) {
            var promise = productList.removeItem(parseInt(this.id));
            promise.then(function (response) {
                console.log("An item has been deleted");
                this.items=response.data;
            },function (error) {
                console.log("An error has occurred while deleting the item:",this.id);
               
            });
            this.onRemove({
                $index: this.items
            });       
        }

ldl274057936 回答:AngularJS组件绑定功能

我会避免使用“ this”。不知道为什么要在RemoveItem fn中传递索引参数,看不到您在此范围内的任何地方使用它。无论如何,如果productList.removeItem返回Promise,我们可以调用onRemove。我将需要更多信息-html代码和您删除的fn代码,但还是请尝试这样做。

  let vm = this;
  vm.RemoveItems = RemoveItem;
  vm.onRemove = onRemove;

  RemoveItem(index) {
                productList.removeItem(parseInt(this.id))
                .then(function (response) {
                    console.log("An item has been deleted");
                    vm.onRemove({$index: response.data});

                },function (error) {
                    console.log("An error has occurred while deleting the item:",this.id);

                });      
            }
  onRemove(obj) {
    console.log(obj); 
//make your remove here
  }
本文链接:https://www.f2er.com/3032956.html

大家都在问