Stack two
Stack two

Reputation: 541

AngularJS using ng-click inside a div

I can't get ng-click to work, I'm trying to pass an url to it inside a div so when the user clicks it goes to the next page, but when I click on the div I can see that it is getting the right link but instead of going to the next page it returns to the home.

<div ng-repeat="contatosCliente in contatos" class="conteudo" type="item-link" ng-click="urlcontato('#/app/contatos/' + contatosCliente.contatos_id)">
                     {{contatosCliente.nome}}
                    <i class="ion-ios-arrow-right" style="border: none; padding-right: 11px"></i>

                </div>

And my controller:

 $scope.urlcontato = function ( path ) {
            $location.path( path );
        };

Upvotes: 1

Views: 687

Answers (3)

ehlers
ehlers

Reputation: 690

As the ionic framework uses ui-router for routing, you should do it this way:

$scope.urlcontato = function ( state ) {    
    $state.go(state);
}

Of course, you have to inject the $state service in your Controller (or directive).

For a default link in your html-code, you should use

<a ui-sref="yourstate">Linktitle</a>

For more information, see this Navigation and Routing Article

Upvotes: 1

ptwo
ptwo

Reputation: 460

Firstly path should always begin with a forward slash (/) if not it is automatically appended. Remove the # and it should work

<div ng-repeat="contatosCliente in contatos" class="conteudo" type="item-link" ng-click="urlcontato('/app/contatos/' + contatosCliente.contatos_id)">
                 {{contatosCliente.nome}}
                <i class="ion-ios-arrow-right" style="border: none; padding-right: 11px"></i>

            </div>

Upvotes: 2

bioball
bioball

Reputation: 1359

$location.path should be called with just a forward slash, without that hash.

https://docs.angularjs.org/api/ng/service/$location

<div ng-repeat="contatosCliente in contatos" class="conteudo" type="item-link" ng-click="urlcontato('/app/contatos/' + contatosCliente.contatos_id)">

And you should check to make sure that your view is bound to your controller. Add some logging in your function, or use a debugger statement, to make sure that urlcontato is being called.

Upvotes: 2

Related Questions