不使用模块显示模式有没有办法在匿名函数中访问"this"?

Sam*_*tar 0 javascript angularjs

如果我有以下代码.是唯一可以this通过使用self变量访问this函数内部的方法,还是有另一种方法可以访问函数内部?

app.controller('ChildCtrl', function($scope, _o) {

  var self=this;
  this.option = {};
  this.option.abc = 25;

  $q.all([_o.getUserProfiles(),
          _u.getConfigs()
        ])
        .then(function (results) {
            ???.option.userProfiles = results[0].userProfiles;
        });
Run Code Online (Sandbox Code Playgroud)

Geo*_*mov 5

你可以使用bind.可能这对你来说会更舒适.

app.controller('ChildCtrl', function($scope, _o) {

  this.option = {};
  this.option.abc = 25;

  $q.all([_o.getUserProfiles(),
          _u.getConfigs()
        ])
        .then(function (results) {
            this.option.userProfiles = results[0].userProfiles;
        }.bind(this));
Run Code Online (Sandbox Code Playgroud)

有关绑定的更多信息,请参阅此处:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind