Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
-6
rated 0 times [  0] [ 6]  / answers: 1 / hits: 42611  / 10 Years ago, wed, march 19, 2014, 12:00:00

I have following three buttons on top of my page with input box underneath it.



<div>
<form>
<div>
Enter Show Name<input type=text ng-model=showName />
</div>
</form>
</div>
<div>
<button ng-click=href=/api/renameShow>Rename Show</button>
<button ng-click=href=/api/updateShow>Update Show</button>
<button ng-click=href=/api/checkShow>Check Show</button>
</div>


My module code with routes is



    var showApp = angular.module(showApp, [ngRoute, ngResource, ui]).
config(function ($routeProvider, $locationProvider) {
$routeProvider.
when('/',
{
controller: '',
templateUrl: 'main.html'
}).
when('/api/renameShow', { controller: 'renameShowCtrl', templateUrl: '/templates/renameShow.html' }).
when('/api/updateShow', { controller: 'updateShowCtrl', templateUrl: '/templates/updateShow.html' }).
when('/api/checkShow', { controller: 'checkShowCtrl', templateUrl: '/templates/checkShow.html' });


Basically what I am trying to do is that when one of the buttons is clicked the ng-click calls the corrosponding page passing the parameter showName with it.
Please let me know how to fix it. Thanks


More From » jquery

 Answers
89

First of all you need to tell your destination controllers (the page you are referring to) to expect and accept a parameter when we navigate to that page:



$routeProvider.when('/api/renameShow/:showName?', { 
controller: 'renameShowCtrl',
templateUrl: '/templates/renameShow.html'
})


The question mark after the parameter denotes that it's an optional parameter. You can achieve the same with anchor tags:



<a href=#/view2/mike>Go to view 2</a>



If you insist on using buttons, write a custom function hooking onto the ng-click of the button, and then pass whatever parameter you want like this from your current controller:



<button ng-click=customNavigate('Mike')>Rename Show</button>


And in the controller:



    $scope.customNavigate=function(msg){
$location.path(/view2+msg)
}


and then in the destination controller:



app.controller(renameShowCtrl,function($scope,$routeParams){
$scope.showName=$routeParams.showName
});

[#71911] Monday, March 17, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nikoguym

Total Points: 339
Total Questions: 106
Total Answers: 95

Location: Mali
Member since Sat, Feb 12, 2022
2 Years ago
nikoguym questions
Wed, Sep 22, 21, 00:00, 3 Years ago
Sun, Dec 13, 20, 00:00, 4 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
;