Javascript和angular中的变量范围问题

Javascript和angular中的变量范围问题,javascript,angularjs,Javascript,Angularjs,在下面的情况下,我如何解决变量范围的问题 Myapp.controller('MyController',['$scope',函数($scope){ var formvalues={}; $scope.somebuttonclick=function(){ mycont.somefunctionB(); }; var mycont={ init:function(){ formvalues.init=“some value init”; 这个; }, somefunction:function

在下面的情况下,我如何解决变量范围的问题

Myapp.controller('MyController',['$scope',函数($scope){
var formvalues={};
$scope.somebuttonclick=function(){
mycont.somefunctionB();
};
var mycont={
init:function(){
formvalues.init=“some value init”;
这个;
},
somefunction:function(){
formvalues.a=“某个值a”;
警报(formvalues.init);//从mycont.init调用时未定义
},
someFunctionB:函数(){
formvalues.b=“某些值”b;
警报(formvalues.init);//从按钮单击调用时,会出现“some value init”
}
};

}]);
您需要将
绑定到函数,或者只需将函数作为对象属性访问,如下所示:

mycont.someFunctionA()

不知道您到底在寻找什么,但请检查以下内容:

Myapp.controller('MyController', ['$scope','$log', function ($scope, $log) {

    var formvalues = {};/*this is empty at first*/

    var mycont = {
        init: function(){
            formvalues.init = "some value init";
        },
        somefunctionA: function(){
            formvalues.a= "some value a";
            /*mycont.init();*/ /*you can call it here, if not in somebuttonclick*/
            $log.log(formvalues.init);
            $log.log(formvalues.a);
        },
        somefunctionB: function(){
            formvalues.b = "some valuee b";
            /* mycont.init(); */ /*you can call it here, if not in somebuttonclick*/
            $log.log(formvalues.init);
            $log.log(formvalues.b);
        }
    };

    $scope.somebuttonclick = function(){
        mycont.init();/*you need to first call this function before you can use the value*/
        mycont.somefunctionA();
        mycont.somefunctionB();
    };
}]);

与此类似。somefunction()…我认为需要通过调用此函数来引用内部函数。否则您需要应用、绑定或调用外部。@Vontei如何应用绑定?