javascript - angular js Unknown provider错误

查看:85
本文介绍了javascript - angular js Unknown provider错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

    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();
        }
    }]);

这段代码会报如下错误:angular.js:12314 Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- $cart;
刚学angular 求大神知道π-π

解决方案

谢邀。
factory、service 只有$rootScope,没办法知道局部的$scope的。

这篇关于javascript - angular js Unknown provider错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆