删除点击的项目angularjs

时间:2014-06-22 16:11:53

标签: angularjs angularjs-scope angular-ui-bootstrap

我有一个click事件,我想从范围中删除具有给定id的元素

<a data-placement="bottom" ng-click='Off(product.productid)'><i class="fa fa-times"></i></a>

app.js

'use strict';

var konfigurator = angular.module('konfiguratorApp', [
    'ngRoute',
    'konfiguratorApp.filters',
    'konfiguratorApp.services',
    'konfiguratorApp.directives',
    'konfiguratorApp.controllers',
    'ui.bootstrap',
    'xeditable',
    'ngSanitize'
], function($interpolateProvider) {
    $interpolateProvider.startSymbol('<%');
    $interpolateProvider.endSymbol('%>');

}).config(['$routeProvider', function($routeProvider) {
        $routeProvider.when('/',
                {
                    templateUrl: '/site/app/partials/products.php',
                    controller: 'Configurator',
                    resolve: {
                        products: function(ConfigurationService) {
                            return ConfigurationService.get();
                        }
                    }
                });

services.js

'use strict';

/* Services */


// Demonstrate how to register services
// In this case it is a simple value service.
angular.module('konfiguratorApp.services', []);

konfigurator.factory("ConfigurationService", function($http) {
  return {
    get: function() {
      return $http.get('/api/products/');
    }
  };
});

Controller.js

angular.module('konfiguratorApp.controllers', [])
        .controller('Configurator', ['$scope', 'products',
            function($scope, products) {
                $scope.products = products.data.data;
                $scope.totalItems = products.data.total;
                $scope.per_page = products.data.per_page;
                $scope.currentPage = products.data.current_page;

            }]);

function Events($scope, $modal, $log) {
    $scope.visible = true;
    $scope.items = ['item1', 'item2', 'item3'];
    $scope.modalEdit = function() {
        var modalInstance = $modal.open({
            templateUrl: '/edit',
            windowClass: 'xx-dialog',
            controller: ModalInstanceCtrl,
            resolve: {
                items: function() {
                    return $scope.items;
                }
            }
        });
    };

    $scope.Off = function(idx) {
        console.log($scope.product);
        $scope.product.slice(idx,1)
        alert($scope.product[idx]);
    };
    $scope.toggle = function() {
        $scope.visible = !$scope.visible;
    };
}

我该怎么做?

更新

我误解了数组切片。我想用id识别目标,但正确的方法是

<a data-placement="bottom" ng-click='Off(product)'><i class="fa fa-times"></i></a>

$scope.Off = function(product) {

    var index = $scope.products.indexOf(product);
    if (index != -1) {
      $scope.products.splice(index, 1);
    }
};

但是,如果我想暂停,我将如何使用产品ID进行restapi更新?

0 个答案:

没有答案