var app=angular.module("myApp",['ng']);
app.factory('$cart',['$scope',function($scope){
return {
add:function(){
$scope.pList.push({price:8000,count:Math.floor(Math.random()*100+1)});
},
del:function(index){
$scope.pList.splice(index,1);
}
}
}]);
app.controller('myCtrl',['$scope','$cart',function($scope,$cart){
$scope.pList=[
{price:2000,count:Math.floor(Math.random()*100+1)},
{price:3000,count:Math.floor(Math.random()*100+1)},
{price:5000,count:Math.floor(Math.random()*100+1)},
{price:9000,count:Math.floor(Math.random()*100+1)}
];
$scope.handleAdd=function(){
$cart.add();
};
$scope.handleDelete=function(){
$cart.del();
}
}]);
This code will report the following error: angular.js:12314 Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- $cart;
Just now Learn angular and ask the master to know π-π
Thanks for the invitation.
factory and service only have $rootScope, so there is no way to know the local $scope.