我正在开发Angularjs SPA应用程序。我有一个div标签,我使用ng-repeat绑定一些细节,如下所示。
<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>
点击div标签(使用class =&#34; grid-container&#34;)我想重定向到新状态。那么如何使用ui-sref和ng-click?我对此感到困惑。有人可以在这方面帮助我吗?谢谢。
答案 0 :(得分:3)
您可以尝试以下代码,
<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>
路径文件应该是:
$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'
});
答案 1 :(得分:1)
向控制器添加goto功能,如下所示:
$scope.goto = function(sref) {
$state.go(sref);
}
通过ng-click调用您的函数:
<div class="car-name" ng-click="goto('ui-sref')">
{{car.make}}
<span class="make">{{car.model}}</span>
</div>