Niranjan Godbole
Niranjan Godbole

Reputation: 2175

How to use ui-sref on clicking of div tag in Angularjs?

I am developing Angularjs SPA application. I have one div tag where I am binding few details using ng-repeat as below.

<div class="grid-container">
    <div class="grid" ng-repeat="car in carDetails">
        <img class="car" src="{{car.WebImageURL}}">
        <div class="car-name">{{car.make}}<span class="make">{{car.model}}</span></div>
        <div class="car-des">lorem ipsum dolor sit amet,</div>
        <div class="car-price">{{car.Price}} <span class="sar">{{car.style}}</span></div>
    </div>
</div>

On clicking on the div tag(with class="grid-container") i want to redirect to new state. So How can I use ui-sref and ng-click? I am little confused about this. Can someone help me in this regard? Thanks.

Upvotes: 2

Views: 2274

Answers (2)

Immanuel Kirubaharan
Immanuel Kirubaharan

Reputation: 1094

You can try like the below code,

<div class="grid-container">
    <div class="grid" ng-repeat="car in carDetails">
        <img class="car" src="{{car.WebImageURL}}">
        <a ui-sref="MainPageRoute.SubPage1Route({name: car.make})"><div class="car-name">{{car.make}}<span class="make">{{car.model}}</span></div></a>
        <a ui-sref="MainPageRoute.SubPage2Route({des: car.desc})"><div class="car-des">lorem ipsum dolor sit amet,</div></a>
        <a ui-sref="MainPageRoute.SubPage3Route({price: car.Price})"><div class="car-price">{{car.Price}} <span class="sar">{{car.style}}</span></div></a>
    </div>
</div>
<ui-view></ui-view>

route file should be like:

$stateProvider.state('MainPageRoute', {
    url: "/MainPage",
    templateUrl: "templates/main-template.html",
    controller: 'main-ctrl'
});

$stateProvider.state('MainPageRoute.SubPage1Route', {
    url: "/SubPage1/:name",
    templateUrl: "templates/sub-page1-template.html",
    controller: 'sub-page1-ctrl'
});

$stateProvider.state('MainPageRoute.SubPage2Route', {
    url: "/SubPage2/:des",
    templateUrl: "templates/sub-page2-template.html",
    controller: 'sub-page2-ctrl'
});

$stateProvider.state('MainPageRoute.SubPage3Route', {
    url: "/SubPage3/:price",
    templateUrl: "templates/sub-page3-template.html",
    controller: 'sub-page3-ctrl'
});

Upvotes: 4

faebzz
faebzz

Reputation: 151

Add a goto function to your controller like this:

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

And call your function via ng-click:

<div class="car-name" ng-click="goto('ui-sref')">
     {{car.make}}
     <span class="make">{{car.model}}</span>
</div>

Upvotes: 1

Related Questions