search

Home  >  Q&A  >  body text

angular js Unknown provider error - Stack Overflow

    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 π-π

女神的闺蜜爱上我女神的闺蜜爱上我2693 days ago685

reply all(1)I'll reply

  • 为情所困

    为情所困2017-06-30 09:57:05

    Thanks for the invitation.
    factory and service only have $rootScope, so there is no way to know the local $scope.

    reply
    0
  • Cancelreply