ahsan ahsan - 15 days ago 7
AngularJS Question

cant access to array outside of controller

<!-- featured items table -->
<ul>
<li ng-repeat="item in items track by item.id">
<a href="" ng-click="addCart(item.id)">{{item.status}}</a>
</li>
</ul>

<!-- another tabs from same data table -->
<ul>
<li ng-repeat="item in items track by item.id">
<a href="" ng-click="addCart(item.id)">{{item.status}}</a>
</li>
</ul>

<script>
// controler
//items array here
// controller end

// start add to cart controller
$scope.addCart = function(id){
$scope.items[id].status = 'added';
}
//end of add to cart controller
</script>


i am new in angular js my code is look like as i wrote above i want to make a function
addtocart
just once and when i click this function i want replace the whole page row where
item.id
match whataever if
id
else where inside
ng-repeat
whatever if id match both
ng-repeat
. when i try to do that i got error about access array and sometime work only for 1 row.
any idea suggestion what i need to do ?
thankyou

regards,
Ahsan Haroon

Answer

The service() function will instantiate the instance using the new keyword when creating the instance.

 var Person = function() {
                var items = [{id:1,title:'test1'},{id:2,title:'test2'}];
                module.service('MyService', function() {
               this.getItems = function() {
                 return items; //.. get your item array
                }

               this.setItems = function() {
               // Update your item array and return it.
               return items;
               }
            });
      };

   angular.service('personService', Person);