1

I'm new to AngularJS, currently I am trying to create a spa for tracking expenses as a simple project but I have some problems with my code.

I managed to do the most of the function but I got stuck at the edit button and edit function. I am trying to make an inline edit function because I already have a form input.

Here is the html and js code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Expenses Tracker</title>
    <link rel="stylesheet" href="bootstrap.min.css">
    <link href="style.css" rel="stylesheet">
</head>
<body>
    <nav class="navbar navbar-default navbar-fixed-top" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <span class="navbar-brand"><img src="logo.png" class="brand" height="40" />
                 Simple Expenses App Tracker</span>
            </div>
        </div>
    </nav>

    <div class="main" ng-app="myList" ng-controller="myListController">

        <img src="logo.png" class="logo">
        
    <form ng-submit="newItem()" class="form">
        <input required type="number" placeholder="Amount" ng-model="amount">
        <input required type="text" placeholder="Description" ng-model="description">
        <input type="submit" value="Submit" class="btn btn-success">
    </form>
        <ul>
            <li ng-repeat="item in items">
                <span  class="pull-left"> {{item.amount}} den.</span> {{item.description}}
                <span class="buttons">
                    <input type="button" value="Edit" class="btn btn-primary" >
                    <input type="button" ng-click="deleteItem($index)" value="Delete" class="btn btn-danger">
                </span>
            </li>
            <br>
            <li><span class="total">{{totalPrice()}} den.</span> <span class="expense"> Total Expenses</span></li>
        </ul>
        
    </div>


    <script src="bootstrap.min.js"></script>
    <script src="angular.min.js"></script>
    <script src="angular-route.min.js"></script>
    <script src="app.js"></script>
</body>
</html>
var myApp = angular.module("myList", []);

myApp.controller("myListController", function($scope) {
    $scope.items = [];

    $scope.newItem = function() {
        $scope.items.push({description:$scope.description, amount: $scope.amount});
        $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;
    };

});


1 Answer 1

1

You could try something like:

$scope.editItem = function(index) {
    $scope.amount = $scope.items[index].amount;
    $scope.description = $scope.items[index].description;
    $scope.deleteItem(index);
}

I also recommend you read this book: https://www.newline.co/ng-book/modern-ng1/

Try to move away from using $scope for bindings and user components. Unless you really don't care where this project goes.

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.