Html 你如何从 href 调用 AngularJS $scope.Function?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17985327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:44:39  来源:igfitidea点击:

How do you call an AngularJS $scope.Function from href?

javascripthtmlangularjs

提问by Sangram Singh

The following works to call a function ActiveChange(variable) from a

以下工作从一个函数调用 ActiveChange(variable)

a(href="javascript: ActiveChange('topStories');") Top Stories

But if the function is defined as an AngularJS $scope function such as

但是如果函数被定义为一个 AngularJS $scope 函数,比如

function activeController ($scope) {    
    $scope.topStories='active';
    $scope.mostRecent='lucky';
    $scope.ActiveChange =   function (activeTab) {
        if(activeTab=='topStories'){
            var x=document.getElementById("someinput");
            x.value='happy to be alive';
            $scope.topStories='active';
            $scope.mostRecent='';
        }
        else {      
            alert('else');
            $scope.topStories='happy';
            $scope.mostRecent='active';
        }
    }
}

How do you call $scope.ActiveChange = function (activeTab) from the ?

你如何调用 $scope.ActiveChange = function (activeTab) ?

回答by Maxim Shoustin

Just use ngClicklike @Mike says. I don't see the reason to use angular in href.

ngClick就像@Mike 说的那样使用。我没有看到在href.

Something like:

就像是:

<a href="" ng-click="ActiveChange('topStories');">Top Stories</a>

Or leave it empty:

或者将其留空:

<a href ng-click="ActiveChange('topStories');">Top Stories</a>