1

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

The local scope variables are only

food: Resource

this: $get.e.$new.a

I can't find $scope and Food

http://jsfiddle.net/fW2EA/1/ http://jsfiddle.net/wizztjh/fW2EA/3/

2 Answers 2

2

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. Food and $scope are in there (as one would expect!).

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I found out if added console.log(Food, $scope); I can access to Food , if I dont use the food. At the debugger I cant acccess to Food service. Angular is so magical ......
1

Within your function, this is the scope, e.g. this.foods.

I believe if you want Food to be accessible add it to the scope, i.e., $scope.Food = Food;.

I'm still a noob and am not sure if adding Food to $scope is the right thing to do.

3 Comments

somehow angular will know whether you use the Food Service anot. If you dont use it, it won't initialize it ..... omg ...
If you're confused about dependency injection (indeed, it does seem like magic :-] ), give this a read: docs.angularjs.org/guide/di
@wizztjh OMG, I was like WTF? Why injected dependencies doesn't appear when I stop using debugger? ... Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.