javascript - angular js Unknown provider錯(cuò)誤
問(wèn)題描述
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();} }]);
這段代碼會(huì)報(bào)如下錯(cuò)誤:angular.js:12314 Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- $cart;剛學(xué)angular 求大神知道π-π
問(wèn)題解答
回答1:謝邀。factory、service 只有$rootScope,沒(méi)辦法知道局部的$scope的。
相關(guān)文章:
1. javascript - 關(guān)于定時(shí)器 與 防止連續(xù)點(diǎn)擊 問(wèn)題2. javascript - 求助關(guān)于js正則問(wèn)題3. objective-c - ios百度地圖定位問(wèn)題4. javascript - 求助這種功能有什么好點(diǎn)的插件?5. javascript - js 有什么優(yōu)雅的辦法實(shí)現(xiàn)在同時(shí)打開的兩個(gè)標(biāo)簽頁(yè)間相互通信?6. 為何 localStorage、sessionStorage 屬于html5的范疇,但是為何 IE8卻支持?7. html5 - rudy編譯sass的時(shí)候有中文報(bào)錯(cuò)8. html - css 如何添加這種邊框?9. javascript - node.js服務(wù)端渲染解疑10. 微信開放平臺(tái) - Android調(diào)用微信分享不顯示
