javascript - How can I route to different URLS in angularjs UI-Router for items in ng-repeat, Need to modify state in ui-router -
i using ui-router , html looks below:
<body ng-controller="myctrl"> <h1>hello plunker!</h1> <ul> <li ng-repeat = "guy in guys"> <a ui-sref="person">{{guy}}{{$index+1}}</a> </li> </ul> </body> the output below:
hello plunker!
file1
file2
file3
and angular code below:
var app = angular.module('app', ['ui.router']); app.controller('myctrl', function($scope) { $scope.val = "this message..." $scope.guys = ['file1','file2','file3'] }); app.config(function($stateprovider, $urlrouterprovider) { $stateprovider .state('person{something needs here}', { url: "/route1", templateurl: "file1.html" }) .state('person{something needs here}', { url: "/route2", templateurl: "file2.html" }) }) can with needs populated here, goal clicking file1 should open file.html , clicking file2 should open file2.html
in short question how open different files/templates/partials when clicking on items repeated in ng-repeat directive , how specify url parameters in state of ui-router
thanks much
i created updated plunker here
state looking this:
.state('guy_route2', { url: "/route/{index:int}", templateprovider: function($templaterequest, $stateparams) { var index = $stateparams.index + 1; var templatename = 'file' + index + '.html'; return $templaterequest(templatename); }, }) this body:
<body ng-controller="myctrl"> <ul> <li ng-repeat = "guy in guys"> <a ui-sref="guy_route2({index: $index})">{{guy}}</a> </li> </ul> <h3>target state</h3> <div ui-view=""></div> </body> see <div ui-view=""></div>, essential target our states. , ui-sref:
ui-sref="guy_route2({index: $index})" where pass state name 'guy_route2', , function call contains object representing state params ({index: $index})
check here
the templateprovider "magic" details found here:
- angular pass paramters in templateurl in stateprovider
- trying dynamically set templateurl in controller based on constant
extend:
with radio adjust like this
<h3>radio</h3> <ul> <li ng-repeat="guy in guys"> <input type="radio" value="{{$index}}" ng-model="model.selected" ng-change="go(model.selected)" id="something{{$index}}" /><label for="something{{$index}}">{{guy}}</label> </li> </ul> and go function
$scope.model = { selected: null }; $scope.go = function(index){ $state.go('guy_route2', {index: index}) ;} }); check here
Comments
Post a Comment