从控制器

时间:2015-11-21 12:07:14

标签: javascript angularjs html5 angularjs-ng-repeat

我必须从控制器中的表中删除一个项目 问题是我不使用范围我使用功能控制器。 如果我尝试添加一个函数,则会崩溃并发出错误。

希望你们能帮助我

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>

所以基本上我只需要知道如何在我的视图和控制器中添加一个功能来删除项目。

1 个答案:

答案 0 :(得分: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>