Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 删除AngularJs上刚创建的对象_Javascript_Angularjs_Typescript_Ecmascript 6 - Fatal编程技术网

Javascript 删除AngularJs上刚创建的对象

Javascript 删除AngularJs上刚创建的对象,javascript,angularjs,typescript,ecmascript-6,Javascript,Angularjs,Typescript,Ecmascript 6,我有一个搜索列表 search.html: 如果我试图删除刚添加的搜索,而不刷新页面,则该搜索无效。 正在调用ng click=“searchesCtrl.deleteSearch(search)” /api/searches/undefined 我尝试在没有$index解决方案的情况下工作。有可能吗?因为新添加的搜索似乎没有\u id参数,因为您只直接在搜索数组中按这个。新闻搜索 基本上,您的addnewpost方法应该返回一个保存在数据库中的对象实体&该实体将由服务器填充正确的\u id。接

我有一个搜索列表

search.html:

如果我试图删除刚添加的搜索,而不刷新页面,则该搜索无效。
正在调用
ng click=“searchesCtrl.deleteSearch(search)”
/api/searches/undefined


我尝试在没有$index解决方案的情况下工作。有可能吗?

因为新添加的
搜索
似乎没有
\u id
参数,因为您只直接在
搜索
数组中按
这个。新闻搜索

基本上,您的addnewpost方法应该返回一个保存在数据库中的对象实体&该实体将由服务器填充正确的
\u id
。接下来,将新实体对象推送到
searches
array。但我个人觉得这种方法非常糟糕,因为我们假设只有一个用户来处理这个系统。因为我们只负责更新javascript中的
搜索
对象

现在我们开始,而不是在本地维护它,我要说的是,您应该重新运行get call来获取所有
搜索
,您已经在执行
$onInit
函数了。因此,它将确保您在UI上看到的列表与服务器同步。删除和保存对象时,必须调用
getSearches
方法,这是正确的方法

代码

class SearchesComponent {
  constructor($http) {
     this.$http = $http;
     this.searches = [];
  }

  getSearches(){
    this.$http.get('/api/searches')
      .then(response => {
        this.searches = response.data;
      });
  }

  $onInit() {
    this.getSearches(); //retrieving list of searches from server
  }

  addNewSearch() {
    if (this.newSearch) {
      this.$http.post('/api/searches', {
        url: this.newSearch.url,
        name: this.newSearch.name
      }).then(() => {
        this.getSearches(); //asking for list from server
      });
    }
  }

  deleteSearch(search) {
    this.$http.delete('/api/searches/' + search._id)
      .then(() => {
        this.getSearches(); //asking for list from server.
      });
  }
}

因为新添加的
search
似乎没有
\u id
参数,因为您只直接在
search
数组中按
this.newSearch

基本上,您的addnewpost方法应该返回一个保存在数据库中的对象实体&该实体将由服务器填充正确的
\u id
。接下来,将新实体对象推送到
searches
array。但我个人觉得这种方法非常糟糕,因为我们假设只有一个用户来处理这个系统。因为我们只负责更新javascript中的
搜索
对象

现在我们开始,而不是在本地维护它,我要说的是,您应该重新运行get call来获取所有
搜索
,您已经在执行
$onInit
函数了。因此,它将确保您在UI上看到的列表与服务器同步。删除和保存对象时,必须调用
getSearches
方法,这是正确的方法

代码

class SearchesComponent {
  constructor($http) {
     this.$http = $http;
     this.searches = [];
  }

  getSearches(){
    this.$http.get('/api/searches')
      .then(response => {
        this.searches = response.data;
      });
  }

  $onInit() {
    this.getSearches(); //retrieving list of searches from server
  }

  addNewSearch() {
    if (this.newSearch) {
      this.$http.post('/api/searches', {
        url: this.newSearch.url,
        name: this.newSearch.name
      }).then(() => {
        this.getSearches(); //asking for list from server
      });
    }
  }

  deleteSearch(search) {
    this.$http.delete('/api/searches/' + search._id)
      .then(() => {
        this.getSearches(); //asking for list from server.
      });
  }
}

您在
函数的哪个部分获得了
未定义的
?对象参数“search.\u id”未定义,因为它刚刚创建。您的服务器是否在
$http.post('/api/searches'..)上返回新创建的对象。然后(newSearch=>…
?您需要以某种方式从服务器获取
搜索。\ id
。是的,您必须在
$http之后获取此id。在您的
addNewSearch
函数中发布
,否则您的
删除
将不起作用。@YuryTarabanko&developer033我的答案中有此id。。但已将其删除,因为我是l。)有点与OP混淆。现在通过查看评论,我确认了我的想法是正确的。因此,我补充了我的答案。:)您在
函数的哪个部分得到了
未定义的
?对象参数“search.\u id”未定义,因为它刚刚创建。您的服务器是否在
$http.post('/api/searches'..)上返回新创建的对象。然后(newSearch=>…
?您需要以某种方式从服务器获取
搜索。\ id
。是的,您必须在
$http之后获取此id。在您的
addNewSearch
函数中发布
,否则您的
删除
将不起作用。@YuryTarabanko&developer033我的答案中有此id。。但已将其删除,因为我是l。)我对OP有点困惑。现在,通过查看评论,我确认了我的想法是正确的。因此,我补充了我的答案。@JulienMalige查看更新的代码。.我稍微改变了我的方法,以前的方法对我来说不合适。.请检查代码并遵循这些步骤,谢谢:)谢谢代码。“我个人仍然觉得这种方法很糟糕”,你的替代方案是什么?刷新吗?@JulienAlige是的,你应该刷新
搜索
,很抱歉混淆了,我的答案中有这个,现在我重新格式化了它..谢谢:)没问题,这对我来说是做我想做的事情的好方法。我正在使用yo angular fullstack生成器和一些教程。代码没有使用相同的实践,有些代码包含bug…@JulienMalige查看更新的代码。我稍微改变了我的方法,以前的方法对我来说不合适。请检查代码并遵循这些步骤,t汉克斯:)谢谢你的密码。“但我个人觉得这种方法很糟糕“,您的替代解决方案是什么?刷新?@JulienMalige是的,你应该刷新
搜索
,很抱歉搞混了,我的答案中有这个,现在我重新格式化了。。谢谢:)没问题,这是我做我想做的事的好方法。我正在使用yo angular fullstack生成器和一些教程。代码没有使用相同的实践,有些代码包含bug。。。
class SearchesComponent {
  constructor($http) {
     this.$http = $http;
     this.searches = [];
  }

  getSearches(){
    this.$http.get('/api/searches')
      .then(response => {
        this.searches = response.data;
      });
  }

  $onInit() {
    this.getSearches(); //retrieving list of searches from server
  }

  addNewSearch() {
    if (this.newSearch) {
      this.$http.post('/api/searches', {
        url: this.newSearch.url,
        name: this.newSearch.name
      }).then(() => {
        this.getSearches(); //asking for list from server
      });
    }
  }

  deleteSearch(search) {
    this.$http.delete('/api/searches/' + search._id)
      .then(() => {
        this.getSearches(); //asking for list from server.
      });
  }
}