简体   繁体   English

从同一控制器内的函数调用函数

[英]Call a function from function inside the same controller

So I'm trying to call a function from another function . 所以我想调用一个functionanother function And both of them defined inside the same Controller . 并且它们都在同一个Controller定义。 But with everything I've tried so far it's "funtion is not defined" at the best. 但是到目前为止我已经尝试过的所有内容都是"funtion is not defined"的最佳状态。 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 ReferenceError:未定义getSomething1

You need to call this.getSomething1() but there's a catch. 你需要调用this.getSomething1()但是有一个问题。

The problem here is that this inside the function is not always the same as this outside it. 这里的问题是, this里面的函数并不总是一样的this外面。 So to be safe, save the controller this in a variable and use that to call the function: 所以为了安全起见,保存控制器this一个变量并用它来调用该函数:

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: 如果您可以使用ES2016语法,则extend调用看起来更干净:

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 使用$scope而不是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) 这样,您可以在控制器中使用getSomething1方法(在js中)和您使用控制器的网页本身(在html中)

You can try using $scope instead of this. 您可以尝试使用$ scope而不是此。

angular.module('App')

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

    $scope.getSomething1 = function() {};

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM