M
M
Maxim Ivanov2016-07-21 10:19:00
JavaScript
Maxim Ivanov, 2016-07-21 10:19:00

How to move functions outside the controller?

There is code, conditional:

// js/controller/MainController'.js
app.controller('MainController', ['$scope', function($scope) { 
$scope.title = 0;

// обработчик
$scope.clickFn = nameFuncion;

}]);

// js/handler/nameFunction.js
function  nameFuncion(){
$scope.title = 1; // не видит $scope, в принципе логично, но можно ли как-то исправить?
}

I just like in C that the function prototype (name) can be described in the class in the header file, and then you can easily read the interface of such a class, and the implementation itself is in the cpp file, can this be done in JS?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Rybakov, 2016-07-21
@splincodewd

This is exactly where all the problems begin when you start using pliers like a hammer, simply because you are used to a hammer.
You can do this:

app.controller('MainController', ['$scope', function($scope) {
  $scope.title = 0;
  // с помощью bind() можно явным образом задать контекст
  $scope.clickFn = nameFuncion.bind($scope);
}]);

function nameFuncion(){
  this.title = 1;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question