Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chapter 1 #19

Open
alfredli opened this issue Feb 26, 2014 · 0 comments
Open

chapter 1 #19

alfredli opened this issue Feb 26, 2014 · 0 comments

Comments

@alfredli
Copy link

the $index in the remove function ,which cannot define as the current index in the ng-repeat array. In the console, when i try to find out the current index, it show undefined.
why?
js:
var myApp=angular.module('app1',[]);
myApp.factory('Data', function(){//this is the set up ofthe service.

 items=[
    {id:0, name:'pen', quality:20, unitPrice:2.20},
    {id:1, name:'paper', quality:17, unitPrice:1.20},
    {id:2, name:'sticker', quality:40, unitPrice:0.10},

]
return items;
})

myApp.controller('Ctrl_1', function($scope, Data){
$scope.items=Data;
$scope.createNewItem=function (a,b,c){
var index=$scope.items.length;
index=index++;

    a=$scope.name;
    b=$scope.quality;
    c=$scope.unitPrice;

     $scope.items.push( {id:index, name:a, quality:b, unitPrice:c});

    }
 $scope.remove = function(index ){
     console.log($scope.items);
     console.log(index);//in concoel show it undefined.
    $scope.items.splice(index, 1);
    console.log($scope.items);
    }

})

myApp.controller('Ctrl_2',function($scope, Data){
$scope.items=Data;
});

html:
<!doctype html>

<title>Angular test 1</title>
create new item
  • {{item.name}}

    Quantity: {{item.quality}}
    Unit Price: {{item.unitPrice}}
    Total: {{item.unitPrice*item.quality | currency}}

    Remove
{{a.message}}
{{a.message}}
    </div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script> <script src="js/app.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant