Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - ng-click doesn't work

Tags:

angularjs

In my view I have:

<button ng-click="getPerson()">test</button>
<ul>
    <li ng-repeat="person in persons">{{ person.name + ' - ' + person.username }}</li>
</ul>

The ul-part works fine, but getPerson() is not called when I click the button? The alert is not shown.

My controller:

app.controller('personsController', function ($scope, personsService) {
getPersons();
function getPersons() {
    personsService.getPersons()
        .success(function (persons) {
            $scope.persons = persons;
        })
        .error(function (error) {
            $scope.status = 'Error: ' + error.message;
        });
}

function getPerson() {
    alert('tst');
}
}); 

Am I doing something wrong?

like image 810
supremo Avatar asked Dec 11 '22 04:12

supremo


1 Answers

The function needs to be on the $scope: Change:

function getPerson() {

To:

$scope.getPerson = function() {
like image 138
tymeJV Avatar answered Jan 01 '23 19:01

tymeJV