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 求大神知道π-π
問題解答
回答1:謝邀。factory、service 只有$rootScope,沒辦法知道局部的$scope的。
相關文章:
1. thinkphp3 count()方法必須加上字段?2. python中return 語句與 分支語句連用問題3. mysql 5個left關鍵 然后再用搜索條件 幾千條數據就會卡,如何解決呢4. python - angular route 與 django urls 沖突怎么解決?5. 這是什么情況???6. 微信內網頁上傳圖片問題7. 非root安裝MySQL5.6報錯,求助!!!8. 輸入地址報以下截圖錯誤,怎么辦?9. node.js - nodejs中mysql子查詢返回多行結果怎么處理?10. mysql - 瞬間流量很高的網站,要頻繁的插入數據到數據庫,應該怎么解決這個問題?
