简体   繁体   English

无法访问控制器中的函数内注入的AngularJS服务

[英]Can't access to injected AngularJS services within a function in a controller

Cannot access to $scope and Food inside destroy function. 无法访问$ scope和Food里面的destroy函数。

foodProject.controller('FoodsCtrl', function($scope, Food) {

    $scope.foods = Food.index();
    $scope.destroy = function(food){
        debugger;
        console.log(Food, $scope);
        food.$destroy();
    };
});

The local scope variables are only 仅限本地范围变量

food: Resource 食物:资源

this: $get.e.$new.a 这个:$ get.e. $ new.a

I can't find $scope and Food 我找不到$ scope和Food

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);

Then in your debugger, check the Closure section of Scope Variables (I was using Chrome), when stopped on your break point. 然后在你的调试器中,检查范围变量的Closure部分(我使用的是Chrome),当你的断点停止时。 Food and $scope are in there (as one would expect!). Food$scope都在那里(正如人们所期望的那样!)。

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. this在破坏功能的上下文是NG重复内的新范围等是不一样的$scope ,虽然两者都是范围。

Within your function, this is the scope, eg this.foods . 在你的功能中, this是范围,例如this.foods

I believe if you want Food to be accessible add it to the scope, ie, $scope.Food = Food; 我相信如果你想要Food可以进入范围,即$scope.Food = Food; .

I'm still a noob and am not sure if adding Food to $scope is the right thing to do. 我仍然是一个菜鸟,我不确定将Food添加到$scope是否正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM