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. javascript - immutable配合react提升性能?2. javascript - sublime快鍵鍵問題3. Apache 已經把網站根目錄的改為allow from all了,但是服務器還是不能訪問?4. javascript - 移動端上不能實現拖拽布局嗎?5. css - 寫頁面遇到個布局問題,求大佬們幫解答,在線等,急!~6. vue.js - Vue 如何像Angular.js watch 一樣監聽數據變化7. 配置Apache時,添加對PHP的支持時語法錯誤8. 實現bing搜索工具urlAPI提交9. phpstudy8.1支持win11系統嗎?10. javascript - nodejs關于進程間發送句柄的一點疑問

網公網安備