如何从href调用AngularJS$scope.Function

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

本文关键字:scope Function AngularJS 调用 href      更新时间:2023-09-26

以下操作用于从调用函数ActiveChange(变量)

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

但是,如果函数被定义为AngularJS$作用域函数,如

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';
        }
    }
}

如何从中调用$scope.ActiveChange=函数(activeTab)?

只需像@Mike所说的那样使用ngClick即可。我看不出在href中使用angular的原因。

类似于:

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

或者清空:

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