Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

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

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 ?

like image 945
Sangram Singh Avatar asked Aug 01 '13 04:08

Sangram Singh


People also ask

What is Ng HREF in AngularJS?

The ng-href directive overrides the original href attribute of an <a> element. The ng-href directive should be used instead of href if you have AngularJS code inside the href value. The ng-href directive makes sure the link is not broken even if the user clicks the link before AngularJS has evaluated the code.

What is function scope in AngularJS?

AngularJS Scope The scope is the binding part between the HTML (view) and the JavaScript (controller). The scope is an object with the available properties and methods. The scope is available for both the view and the controller.

What is scope watch in AngularJS?

The angular JS $watch function is used to watch the scope object. The $watch keep an eye on the variable and as the value of the variable changes the angular JS $what runs a function. This function takes two arguments one is the new value and another parameter is the old value.

How does $scope work in angular?

The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).


1 Answers

Just use ngClick like @Mike says. I don't see the reason to use angular in 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>
like image 75
Maxim Shoustin Avatar answered Sep 25 '22 01:09

Maxim Shoustin