supremo
supremo

Reputation: 151

Angular - ng-click doesn't work

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?

Upvotes: 4

Views: 13619

Answers (1)

tymeJV
tymeJV

Reputation: 104795

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

function getPerson() {

To:

$scope.getPerson = function() {

Upvotes: 12

Related Questions