0

I'm trying to figure out how to edit a scope, i can successfully add and remove a object, in this example im developing a simple todo app.

I was following this tutorial

i referenced a similar question on this site, but nothing similar to what im looking for

How do you edit a scope ?

<!DOCTYPE html>
<html ng-app="eli">
<head
    <meta charset="UTF-8">
    <title>Untitled Bullcrap</title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
    <script src="main.js"></script>
</head>
<body>

<div ng-controller="mycontroller">

<ul>
    <li ng-repeat ="x in products">{{x}}
    <span ng-click="removeItem($index)">&times;</span>
    <button ng-click="selectProduct(x)">Edit</button>
    </li>
</ul>

<input ng-model="addMe">
<button ng-click="addItem()">Add</button>

</div>

</body>
</html>

here is the angularjs script.

angular.module('eli', [])
    .controller('mycontroller', function($scope){

// My code
        $scope.products = ["Milk", "Bread", "Cheese"];
        $scope.newProduct = {};
        $scope.addItem = function(){
            $scope.products.push($scope.addMe);
        }

        $scope.saveProduct = function(x)
        {
            $scope.products.push($scope.newProduct);
            $scope.newProduct = {};
        }

        $scope.selectProduct = function(x){
            $scope.clickedProduct = x;
        }

        $scope.removeItem = function(x){
            $scope.products.splice(x,1);
        }


        $scope.editItem = function(x){
            $scope.editItem = true;


        }




    });
2
  • 1
    What do you mean by "edit" a scope? Commented Oct 26, 2017 at 1:58
  • @TimBiegeleisen edit a product in this example and update it. Commented Oct 26, 2017 at 1:59

1 Answer 1

1

angular.module('eli', [])
    .controller('mycontroller', function($scope){

// My code
        $scope.products = ["Milk", "Bread", "Cheese"];
        $scope.newProduct = {};
        $scope.editIndex=-1
        $scope.addItem = function(){
            $scope.products.push($scope.addMe);
        }

        $scope.saveProduct = function(x)
        {
            $scope.products.push($scope.newProduct);
            $scope.newProduct = {};
        }

        $scope.editProduct = function(x){
            $scope.editIndex = x;
            $scope.addMe=$scope.products[x];
        }

        $scope.updateItem=function(){
        	$scope.products[$scope.editIndex]=$scope.addMe;
        	$scope.editIndex = -1;
        	$scope.addMe="";
        }

        $scope.removeItem = function(x){
            $scope.products.splice(x,1);
        }
    });
<!DOCTYPE html>
<html ng-app="eli">
<head
    <meta charset="UTF-8">
    <title>Untitled Bullcrap</title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
</head>
<body>

<div ng-controller="mycontroller">

<ul>
    <li ng-repeat ="x in products">{{x}}
    <span ng-click="removeItem($index)">&times;</span>
    <button ng-click="editProduct($index)">Edit</button>
    </li>
</ul>

<input ng-model="addMe">
<button ng-click="addItem()" ng-if="editIndex==-1">Add</button>
<button ng-click="updateItem()" ng-if="editIndex>-1">Update</button>

</div>
</body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.