0

I am trying to use scope inside this function but it seems to not working. Where should I put the argument $scope.

Thank you

Here is the structure of the code I put the main lines that reflect the structure :

myApp.controller('myCtrl', ['$scope','Fact', function($scope,Fact) {
   $scope.myFunc = function() {

      Fact.init(beamNumber,function(){
         polygonsBeam[ list.length - 1- i].addListener('click',function(e) {
            console.log($scope.db)
            $scope.db = 20
            console.log(list[list.length - 1 - polygonsBeam.indexOf(this)]);
         });
      });
   };
}]);
6
  • What's the context? Is this inside some controller? If so, are you injecting $scope into it? Commented Oct 20, 2017 at 17:32
  • Thanks for your answer. Yes it is inside a controller myApp.controller('myCtrl', ['$scope','Fact', function($scope,Fact) { ... Commented Oct 20, 2017 at 17:34
  • I think I did inject Commented Oct 20, 2017 at 17:35
  • Then the $scope object should be part of the closure and thus accessible. When you say it's "not working", what do you mean? Is your click function not being called at all? Is it being called but it's not doing what you expect? Commented Oct 20, 2017 at 17:41
  • I edited the code in the question Commented Oct 20, 2017 at 17:54

1 Answer 1

1

I don't know what Fact.init is doing, but if you see your console.log but $scope.db doesn't seem to be updated, try:

$scope.$apply(function(){
   $scope.db = 20
})

From https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$apply

$apply() is used to execute an expression in AngularJS from outside of the AngularJS framework. (For example from browser DOM events, setTimeout, XHR or third party libraries)

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

4 Comments

Thanks a lot for your help. It worked. I appreciate it :)
Happy to help you! Please accept the answer if it helps.
Actually, what if I want to use a global variable inside my function that is not declared in the event ? $scope.db = myglobalVariable
If it's a global variable you can use anywhere you want.

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.