AngularJS控制器实例方法
AngularJS Controller instance method
我有一个 angular js 控制器,我正在以某种方式编写它,以便其他控制器可以继承它。我没有在 angular 的控制器函数中将函数定义为单个函数,而是按以下方式编写:
function SomeCtrl($scope)
{
this.some_field = "1234";
this.scope.externalMethod = angular.bind(this, this.externalMethod);
this.scope.anotherMethod = angular.bind(this, this.anotherMethod);
return this;
}
SomeCtrl.prototype.externalMethod = function()
{
//Do something
//....(Don't worry about this method, its just to highlight that I can call this method from $scope.externalMethod)
}
//These are the methods of interest
SomeCtrl.prototype.instanceMethodOne = function()
{
//Do something....
}
SomeCtrl.prototype.anotherMethod = function()
{
this.instanceMethodOne(); //---> Problem here!
//Carry on
//....
}
angular.module('some_module') //Previously defined
.controller('SomeCtrl', SomeCtrl)
所以我现在遇到的问题是在方法 "anotherMethod" 中有一个引用 (this),它调用 class 实例方法 "instanceMethodOne"。这解析为 null,因为此时未解析自引用 "this"。有没有办法像这种情况一样在其实例方法中引用对象?
问题已解决
更新并修复:
好的,我已经调查了这个问题。我实际上应该更新问题。它实际上看起来更像这样:
function SomeCtrl($scope)
{
this.some_field = "1234";
this.scope.externalMethod = angular.bind(this, this.externalMethod);
this.scope.anotherMethod = angular.bind(this, this.anotherMethod);
return this;
}
SomeCtrl.prototype.externalMethod = function()
{
//Do something
//....(Don't worry about this method, its just to highlight that I can call this method from $scope.externalMethod)
}
//These are the methods of interest
SomeCtrl.prototype.instanceMethodOne = function()
{
//Do something....
}
SomeCtrl.prototype.anotherMethod = function()
{
var aCallBack = function()
{
//Some stuff...
this.instanceMethodOne(); //---> Problem here!
}
//Carry on
//....
}
angular.module('some_module') //Previously defined
.controller('SomeCtrl', SomeCtrl)
因此解决方法是首先创建对对象方法的引用。这样我就可以在回调函数中调用它。回调函数中的 "this" 指的是该函数本身。抱歉误报...现在已修复!感谢@sma 的调查。
我有一个 angular js 控制器,我正在以某种方式编写它,以便其他控制器可以继承它。我没有在 angular 的控制器函数中将函数定义为单个函数,而是按以下方式编写:
function SomeCtrl($scope)
{
this.some_field = "1234";
this.scope.externalMethod = angular.bind(this, this.externalMethod);
this.scope.anotherMethod = angular.bind(this, this.anotherMethod);
return this;
}
SomeCtrl.prototype.externalMethod = function()
{
//Do something
//....(Don't worry about this method, its just to highlight that I can call this method from $scope.externalMethod)
}
//These are the methods of interest
SomeCtrl.prototype.instanceMethodOne = function()
{
//Do something....
}
SomeCtrl.prototype.anotherMethod = function()
{
this.instanceMethodOne(); //---> Problem here!
//Carry on
//....
}
angular.module('some_module') //Previously defined
.controller('SomeCtrl', SomeCtrl)
所以我现在遇到的问题是在方法 "anotherMethod" 中有一个引用 (this),它调用 class 实例方法 "instanceMethodOne"。这解析为 null,因为此时未解析自引用 "this"。有没有办法像这种情况一样在其实例方法中引用对象?
问题已解决
更新并修复:
好的,我已经调查了这个问题。我实际上应该更新问题。它实际上看起来更像这样:
function SomeCtrl($scope)
{
this.some_field = "1234";
this.scope.externalMethod = angular.bind(this, this.externalMethod);
this.scope.anotherMethod = angular.bind(this, this.anotherMethod);
return this;
}
SomeCtrl.prototype.externalMethod = function()
{
//Do something
//....(Don't worry about this method, its just to highlight that I can call this method from $scope.externalMethod)
}
//These are the methods of interest
SomeCtrl.prototype.instanceMethodOne = function()
{
//Do something....
}
SomeCtrl.prototype.anotherMethod = function()
{
var aCallBack = function()
{
//Some stuff...
this.instanceMethodOne(); //---> Problem here!
}
//Carry on
//....
}
angular.module('some_module') //Previously defined
.controller('SomeCtrl', SomeCtrl)
因此解决方法是首先创建对对象方法的引用。这样我就可以在回调函数中调用它。回调函数中的 "this" 指的是该函数本身。抱歉误报...现在已修复!感谢@sma 的调查。