Function call from angular

How can I call a function in my controller from the $ scope.watch function? I try something like this, but it does not work.

$scope.$watch(function(){return self.user}, function (newVal, oldVal, scope){
if (self.user) {
    getNotificationCount();
  } 
});

var getNotificationCount = function(){
  console.log("called your function");
}

it gives me an error

TypeError: getNotificationCount is not a function
+4
source share
4 answers

You need to determine getNotificationCountbefore calling it.

var getNotificationCount = function(){
  console.log("called your function");
}

$scope.$watch(function(){return self.user}, function (newVal, oldVal, scope){
  if (self.user) {
    getNotificationCount();
  } 
});
+1
source

Declare a function in the angular scope:

$scope.getNotificationCount = function(){
  console.log("called your function");
}
+1
source

- . , , .

getNotificationCount, . , , , , , getNotificationCount() {};

0
$scope.$watch(function(){return self.user}, function (newVal, oldVal, scope){
  if (self.user) {
   getNotificationCount();
  } 
});

var getNotificationCount = function(){
 console.log("called your function");
}

, getNotificationCount, . , , $scope. $Watch code block , , getNotificationCount.

getNotificationCount ,

 function getNotificationCount() { 
   console.log('calling your function') 
 }

. , . , , $scope. $watch(). , , , , $scope. $Watch, getNotificationCount .

, var getNotificationCount, $scope. $watch ( , )

0

Source: https://habr.com/ru/post/1661906/


All Articles