簡體   English   中英

我從列表中刪除一個項目,同樣的項目也從AngularJs的另一個列表中刪除

[英]I delete one item from list,also same item is deleted from another list in AngularJs

我有兩個列表,兩個都充滿了我的服務,我的服務是這樣的:

我的服務:

 `function data (){

   var locationList={
           list: ''
   }
   var currentlocation = {
         editList : ''
   }

return {


       getAllLocationList:function(){

           return locationList.list;
       },

      setAllLocationList:function(allLocationList){
          locationList.list = allLocationList; 
       },

       getCurrentList:function(){

           return currentlocation.editList;
       },

      setCurrentList:function(currentList){
          currentlocation.editList = currentList; 
       },

  }
 }`

在我的控制器中

vm.location=data.getCurrentList();
vm.allLocationList=data.getAllLocationList();

vm.location包含ID,名稱,關鍵字,數字,而vm.allLocationList包含名稱,關鍵字,數字

我也有keywordAdd和keywordDelete函數。當我輸入keywordDelete函數並從vm.location列表中刪除一些關鍵字時,然后自動從vm.allLocationList刪除相同的項目。同樣的情況對keywordAdd函數有效。 你有什么想法嗎?也許服務結構不好,我是第一次寫這個。 我已經工作了兩天了,還沒有解決。

因此,這是關於Javascript執行上下文的 創建函數時,函數內部的變量在上下文中屬於它。 這意味着它們對於data()每個實例都是相似的。

Javascript(我認為自ES5起)支持面向對象的編程,可讓您創建一個對象,然后創建多個(半)分離的實例。

function Data() {
   this.locationList={
           list: ''
   }
   this.currentlocation = {
         editList : ''
   }
}

Data.prototype = {
       getAllLocationList:function(){
           return this.locationList.list;
       },
      setAllLocationList:function(allLocationList){
          this.locationList.list = allLocationList; 
       },
       getCurrentList:function(){
           return this.currentlocation.editList;
       },
      setCurrentList:function(currentList){
          this.currentlocation.editList = currentList; 
       },
 }

var data = new Data();

我強烈建議您進一步閱讀該主題。 它是許多JS混淆的核心。 祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM