2
如何在下面的情况下解决变量作用域的问题。Javascript和角度变量作用域问题
Myapp.controller('MyController', ['$scope', function ($scope) {
var formvalues = {};
$scope.somebuttonclick = function(){
mycont.somefunctionB();
};
var mycont = {
init: function(){
formvalues.init = "some value init";
this.somefunctionA();
},
somefunctionA: function(){
formvalues.a= "some value a";
alert(formvalues.init); //comes undefined when called from mycont.init
},
somefuntionB: function(){
formvalues.b = "some valuee b";
alert(formvalues.init); // comes "some value init" when called from buttonclick
}
};
}]);
通过点击按钮调用,变量正确定义,而是mycont方法中调用的时候,它说不确定。如何解决这个
类似this.someFunctionA()...我想你需要通过调用这个引用你的内部函数。否则,您需要申请,绑定或致电外部。 – Vontei
@Vontei如何申请绑定? –