一个函数内不能访问到注入AngularJS服务控制器 [英] Can't access to injected AngularJS services within a function in a controller
问题描述
无法访问到$范围和食物中破坏的功能。
Cannot access to $scope and Food inside destroy function.
foodProject.controller('FoodsCtrl', function($scope, Food) {
$scope.foods = Food.index();
$scope.destroy = function(food){
debugger;
console.log(Food, $scope);
food.$destroy();
};
});
局部范围变量仅
食品:资源
本:$ get.e. $ new.a
this: $get.e.$new.a
我找不到$范围和食物
http://jsfiddle.net/fW2EA/1/
http://jsfiddle.net/wizztjh/fW2EA/3/
http://jsfiddle.net/fW2EA/1/ http://jsfiddle.net/wizztjh/fW2EA/3/
推荐答案
包含此行的破坏功能(它是从你的提琴缺失):
Include this line in your destroy function (it is missing from your fiddle):
console.log(Food, $scope);
然后在你的调试器,检查范围变量的合龙段(我使用Chrome),在你的破发点时停止。 食物
和 $范围
都在那里(如人们所期望的!)。
Then in your debugger, check the Closure section of Scope Variables (I was using Chrome), when stopped on your break point. Food
and $scope
are in there (as one would expect!).
这个
中的破坏功能是NG重复内的一个新的范围,以不一样的 $范围上下文
,虽然两者都是范围。
this
in the context of the destroy function is a new scope within the ng-repeat so is not the same as $scope
, although both are scopes.
这篇关于一个函数内不能访问到注入AngularJS服务控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!