最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Call a function from function inside the same controller - Stack Overflow

programmeradmin1浏览0评论

So I'm trying to call a function from another function. And both of them defined inside the same Controller. But with everything I've tried so far it's "funtion is not defined" at the best. How to do this thing properly?

angular.module('App')

.controller('Controller', ['$http', '$scope', function($http, $scope) {

    this.getSomething1 = function() {};

    this.getSomething2 = function() {
        if (1 == 1) {
            getSomething1();
        }
    };
}]);

ReferenceError: getSomething1 is not defined

So I'm trying to call a function from another function. And both of them defined inside the same Controller. But with everything I've tried so far it's "funtion is not defined" at the best. How to do this thing properly?

angular.module('App')

.controller('Controller', ['$http', '$scope', function($http, $scope) {

    this.getSomething1 = function() {};

    this.getSomething2 = function() {
        if (1 == 1) {
            getSomething1();
        }
    };
}]);

ReferenceError: getSomething1 is not defined

Share Improve this question edited Jun 27, 2016 at 13:31 Mahendra Kulkarni 1,5073 gold badges26 silver badges37 bronze badges asked Jun 27, 2016 at 12:04 ottercoderottercoder 8621 gold badge12 silver badges32 bronze badges 4
  • 1 I'm assuming you tried with this.getSomething1();? I usually define my functions on the $scope though.. $scope.getSomething1 = function() { ... } and just call it by $scope.getSomething1();. – Arg0n Commented Jun 27, 2016 at 12:06
  • @Arg0n, yes. And it gives me TypeError: this.getSomething1 is not a function – ottercoder Commented Jun 27, 2016 at 12:09
  • You should put other functions in a service, it's bad design to clutter controller methods with too much code – Marko Commented Jun 27, 2016 at 13:34
  • And instead of this I would bind it too $scope – Marko Commented Jun 27, 2016 at 13:45
Add a comment  | 

4 Answers 4

Reset to default 8

You need to call this.getSomething1() but there's a catch.

The problem here is that this inside the function is not always the same as this outside it. So to be safe, save the controller this in a variable and use that to call the function:

angular.module('App')

.controller('Controller', ['$http', '$scope', function ($http, $scope) {
    var vm = this;
    vm.getSomething1 = function () {
    };

    vm.getSomething2 = function ()  {
        if(1 == 1){
            vm.getSomething1();
        }
    };
}
]);

Another option that can make for much cleaner code is to always use named functions. You can still expose whichever of them need to be exposed on the controller but you can also call them directly.

angular.module('App')

.controller('Controller', ['$http', '$scope', function ($http, $scope) {
    angular.extend(this, { getSomething1: getSomething1, getSomething2: getSomething2 });
    return;

    function getSomething1() {
    };

    function getSomething2()  {
        if(1 == 1){
            getSomething1();
        }
    };
}
]);

This also has the benefit of separating the initialisation code at the top of the controller instead of scattering it through the function declarations.

The extend call looks even cleaner if you can use ES2016 syntax:

angular.extend(this, { getSomething1, getSomething2 });

Try using the scope variable instead of this in the controllers,

angular.module('App')

.controller('Controller', ['$http', '$scope', function ($http, $scope) {
    var scope = $scope
    scope.getSomething1 = function () {
    };

    scope.getSomething2 = function ()  {
        if(1 == 1){
            scope.getSomething1();
        }
    };
}
]);

You can also declare a controller using a functional syntax as,

(function() {
  'use strict';

  angular
    .module('App')
    .controller('Controller', Controller);

  /** @ngInject */
    function Controller($http, $scope) {

        var scope = $scope
        scope.getSomething1 = function () {
        };

        scope.getSomething2 = function ()  {
            if(1 == 1){
                scope.getSomething1();
            }
        };
    }


})();    

Use $scope instead of this

angular.module('App')

.controller('Controller', ['$http', '$scope', function($http, $scope) {

    $scope.getSomething1 = function() {};//<=== use of $scope

    this.getSomething2 = function() {
        if (1 == 1) {
            $scope.getSomething1(); //<=== use of $scope
        }
    };
}]);

That way, you can use the getSomething1 method in the controller (in js) and in your web page itself where you use your controller (in html)

You can try using $scope instead of this.

angular.module('App')

.controller('Controller', ['$http', '$scope', function($http, $scope) {

    $scope.getSomething1 = function() {};

    $scope.getSomething2 = function() {
        if (1 == 1) {
            $scope.getSomething1();
        }
    };
}]);
发布评论

评论列表(0)

  1. 暂无评论