2015-11-21 93 views
0

我必須從我的控制器中的表中刪除項目 問題是我不使用範圍我使用功能控制器。 如果我嘗試添加一個函數會崩潰並出錯。從控制器刪除項目

希望你們能幫助我

的HomeController:

// homeController.js 


(function() { 
    'use strict'; 
    angular.module('myApp') 
     .controller('homeController', homeController); 


    function homeController() { 
     var vm = this; 
     vm.autos = [ 
      {id: 1, 'Omschrijving': 'A 180', Prijs: '24.242 € ', Type: 'A'}, 
      {id: 2, 'Omschrijving': 'A 180 CDI BlueEFFICIENCY', Prijs: '26.015 € ', Type: 'A'}, 
      {id: 3, 'Omschrijving': 'B 200 CDI BlueEFFICIENCY', Prijs: '30.129 € ', Type: 'B'}, 
      {id: 4, 'Omschrijving': 'C 250 CGI BlueEFFICIENCY', Prijs: '40.414 € ', Type: 'C'}, 
      {id: 5, 'Omschrijving': 'C 300 CDI 4MATIC BlueEFFICIENCY', Prijs: '48.642 € ', Type: 'C'}, 
      {id: 6, 'Omschrijving': 'C 350 CGI 4MATIC BlueEFFICIENCY', Prijs: '50.941 € ', Type: 'C'}, 
      {id: 7, 'Omschrijving': 'CL 500 CDI 4MATIC BlueEFFICIENCY', Prijs: '129.954 € ', Type: 'CL'}, 
      {id: 8, 'Omschrijving': 'CL 600', Prijs: '170.489 € ', Type: 'CL'} 
     ]; 
    } 


}; 
})(); 

home.html的

<div> 
    <table style="text-align: left;"> 
    <th> 
    Omschrijving 
    </th> 
    <th> 
    Prijs 
    </th> 
    <th> 
    Type 
    </th> 
     <tr ng-repeat="autos in homeCtrl.autos"> 
     <td width="75%"> 
      <a ng-href="#/detail/{{autos.id}}"> 
      {{ autos.Omschrijving }} 
      </a> 
      </td> 
      <td width="25%"> 
      {{ autos.Prijs }} 
      </td> 
      <td width="25%"> 
      {{ autos.Type}} 
      </td> 
      <td> 
      <a ng-click="delete(something)">Delete</a> 
      </td> 
     </tr> 
    </table> 
    <a href="#/add">Auto Toevoegen</a> 

</div> 

所以basicly我只需要知道我可以在我看來,增加一個功能,我的控制器刪除一個項目。

+0

你嘗試過什麼? –

+0

@MadaraUchiha 好,是啊試圖與範圍一切工作,但在我的控制器比錯誤。應該只有一種方式只有問題是我不知道 –

回答

0
// homeController.js 


(function() { 
    'use strict'; 
    angular.module('myApp') 
     .controller('homeController', homeController); 


    function homeController() { 
     var vm = this; 
     vm.autos = [ 
      {id: 1, 'Omschrijving': 'A 180', Prijs: '24.242 € ', Type: 'A'}, 
      {id: 2, 'Omschrijving': 'A 180 CDI BlueEFFICIENCY', Prijs: '26.015 € ', Type: 'A'}, 
      {id: 3, 'Omschrijving': 'B 200 CDI BlueEFFICIENCY', Prijs: '30.129 € ', Type: 'B'}, 
      {id: 4, 'Omschrijving': 'C 250 CGI BlueEFFICIENCY', Prijs: '40.414 € ', Type: 'C'}, 
      {id: 5, 'Omschrijving': 'C 300 CDI 4MATIC BlueEFFICIENCY', Prijs: '48.642 € ', Type: 'C'}, 
      {id: 6, 'Omschrijving': 'C 350 CGI 4MATIC BlueEFFICIENCY', Prijs: '50.941 € ', Type: 'C'}, 
      {id: 7, 'Omschrijving': 'CL 500 CDI 4MATIC BlueEFFICIENCY', Prijs: '129.954 € ', Type: 'CL'}, 
      {id: 8, 'Omschrijving': 'CL 600', Prijs: '170.489 € ', Type: 'CL'} 
     ]; 

     vm.delete = function(id){ 
      // delete vm.autos[id]; 
      vm.autos.splice(id-1, 1); // this is better 
     } 
    } 



})(); 

home.html的

<div> 
    <table style="text-align: left;"> 
    <th> 
    Omschrijving 
    </th> 
    <th> 
    Prijs 
    </th> 
    <th> 
    Type 
    </th> 
     <tr ng-repeat="autos in homeCtrl.autos"> 
     <td width="75%"> 
      <a ng-href="#/detail/{{autos.id}}"> 
      {{ autos.Omschrijving }} 
      </a> 
      </td> 
      <td width="25%"> 
      {{ autos.Prijs }} 
      </td> 
      <td width="25%"> 
      {{ autos.Type}} 
      </td> 
      <td> 
      <a ng-click="homeCtrl.delete(autos.id)">Delete</a> 
      </td> 
     </tr> 
    </table> 
    <a href="#/add">Auto Toevoegen</a> 

</div> 
+0

那麼,唯一的問題是我不能按兩次,如果我想刪除一個其他車,它會給出錯誤: 重複在中繼器是不允許的。使用'track by'表達式來指定唯一鍵。 Repeater:homeCtrl.autos中的自動,重複鍵:undefined:undefined,Duplicate value:undefined –

+0

如果存在冗餘ID,則可以在ng-repeat中使用「track by」。 即使用 而不是 – Indrajith

+0

不允許在中繼器中重複複製。使用'track by'表達式來指定唯一鍵。 Repeater:homeCtrl.autos跟蹤autos.id中的自動,重複鍵:undefined,重複值:undefined 仍然是同樣的錯誤 –