Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

AngularJS two different actions in ng-submit

var myApp = angular.module("myList", []);

myApp.controller("myListController", function($scope) {
    $scope.items = [];
    $scope.isEdit = false; // initialize
    $scope.editingIndex = null;  //initialize
    $scope.newItem = function() {
     if(!$scope.isEdit){  //if not in edit mode -> add new
       $scope.items.push({description:$scope.description, amount: $scope.amount});

     }
      else{  //in edit mode -> edit the object
        $scope.items[$scope.editingIndex] = {  //replace with new values
          description:$scope.description, amount: $scope.amount
        }
        $scope.isEdit = false;    //setting back to false
        $scope.editingIndex = null;
      }
            $scope.description = '';
            $scope.amount = 0
    };

    $scope.deleteItem = function(index) {
        $scope.items.splice(index, 1);
    }

    $scope.totalPrice = function() {
        var total = 0;
        for(count=0; count<$scope.items.length;count++){
            total += $scope.items[count].amount;
        }
        return total;
    };
 
PREVIOUS NEXT
Tagged: #AngularJS #actions
ADD COMMENT
Topic
Name
4+7 =