0

Hello I'm new in angularJS. Suitable or not to implement function inside function?

For example like this:-

$scope.loadDistrict = function(id) {
  // statement
  $scope.loadBasedOnYear = function(y_id) {
    console.log(y_id);
    // statement
  };
};

3 Answers 3

3

If you bind method on scope, it's available from view.

From your code

$scope.loadDistrict = function(id) {
  // statement
  $scope.loadBasedOnYear = function(y_id) {
    console.log(y_id);
    // statement
  };
};

loadBasedOnYear won't available until loadDistrict is called.

It's very bad pattern to follow.

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

Comments

0

It is possible but without context I don't really know why you would do this, calling $scope.loadBasedOnYear before calling $scope.loadDistrict will result in an error so be careful with such a design pattern.

Comments

0

Yes this is fine.

You should watch out for when the function will be executed outside of angular's cycle. E.g. if you do:

setTimeout(function() { $scope.test = 5; }, 1000);

If you need to do this then you need to wrap the function in $scope.$apply(), or use $timeout.

Comments

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.