Service Portal says "$scope is not defined"

amwebaz
Kilo Expert

When I run the following code  in my Client Script on the Service Portal. The console reports: $scope is not defined.

Does anyone have knowledge on this?

 

(function($location) {
/* widget controller */
var c = this;

function checkLocation() {
var loc = $location.search();
if (loc.table == 'sc_cat_item') {
alert("works");
}
}


$scope.$on('$locationChangeSuccess',function() {
checkLocation();
});

checkLocation();
})();

1 ACCEPTED SOLUTION

Hi amwebaz,

 

$scope should be an argument of client controller, and the function should not be immediately invoked:

function($scope, $location) {
  /* widget controller */
  var c = this;
  function checkLocation() {
    var loc = $location.search();
    if (loc.table == 'sc_cat_item') {
      alert("works");
    }
  }

  $scope.$on('$locationChangeSuccess',function() {
    checkLocation();
  });
  checkLocation();
}

View solution in original post

5 REPLIES 5

Bhagya Lakshmi
Mega Guru

hi,

(function($scope,$location) {
/* widget controller */
var c = this;

function checkLocation() {
var loc = $location.search();
if (loc.table == 'sc_cat_item') {
alert("works");
}
}


$scope.$on('$locationChangeSuccess',function() {
checkLocation();
});

checkLocation();
})();

 

try once.

Thanks that worked, but now I get the following:

TypeError: Cannot read property '$on' of undefined

 

 

(function($scope, $location) {
/* widget controller */
var c = this;

function checkLocation() {
var loc = $location.search();
if (loc.table == 'sc_cat_item') {
alert("works");
}
}


$scope.$on('$locationChangeSuccess',function() {
checkLocation();
});

checkLocation();
})();

Check $rootScope in the place of $scope

Hi amwebaz,

 

$scope should be an argument of client controller, and the function should not be immediately invoked:

function($scope, $location) {
  /* widget controller */
  var c = this;
  function checkLocation() {
    var loc = $location.search();
    if (loc.table == 'sc_cat_item') {
      alert("works");
    }
  }

  $scope.$on('$locationChangeSuccess',function() {
    checkLocation();
  });
  checkLocation();
}