我想更新状态信息,我需要传递两个参数,一个是school_id,另一个是状态,可能是(已批准,待定,拒绝),具体取决于用户点击按钮。我有一个类似school_info的表,其中包含 id school_name 状态等字段所以在我的applicationjs中我需要传递两个params id和status。这是我的HTML代码,我有重复,所以我在学校使用学校。
<a href="#/changeStatus/{{school.id}}/approved"><button class="btn btn-success" ng-model="approve">Approve</button></a>
<a href="#/changeStatus/{{school.id}}/declined"><button class="btn btn-danger" ng-model="decline">Decline</button><a>
<a href="#/changeStatus/{{school.id}}/pending"><button class="btn btn-deafult" ng-model="Pending">Pending</button></a>
我的app.js代码是
var app=angular.module("admin",[]);
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'home.php',
controller: 'adminCtrl'
}).when('/changeStatus/:id/:sts',{
templateUrl:'home.php',
controller:'adminCtrl'
}).when('/Mapview/:id',{
templateUrl:'mapview.html',
controller:'MapCtrl'
}).otherwise({
redirectTo: "/"
});
});
app.controller("adminCtrl",function($scope,$http,$routeParams){
var sts=$routeParams.status;
var id=$routeParams.id;
console.log("sts"+sts+id);
//here I want two params to be fetched inside console log. when clicked on specific btn.I have seen previous questions asked on multiple params passing but there must be easy way out to get multiple params.
});
答案 0 :(得分:1)
请使用ng-href
代替href
你的hrefs应如下所示,不需要:
<a ng-href="#/changeStatus/{{school.id}}/approved"><button class="bt......
<a ng-href="#/changeStatus/{{school.id}}/declined"><button class="btn btn...
然后在控制器中,
var sts=$routeParams.sts; // u need to get as sts ,
//because you get it as sts in .when('/changeStatus/:id/:sts'..
var id=$routeParams.id;
console.log("sts"+sts);
console.log("id"+id);
答案 1 :(得分:0)
您的链接不正确。
在AngularJS文档中,他们有这个例子:
// Given:
// URL: http://server.com/index.html#/Chapter/1/Section/2?search=moby
// Route: /Chapter/:chapterId/Section/:sectionId
//
// Then
$routeParams ==> {chapterId:'1', sectionId:'2', search:'moby'}
我没有测试它,但我认为您需要做的唯一更改是从链接中删除双点,如下所示:
<a href="#/changeStatus/{{school.id}}/approved">
<button class="btn btn-success" ng-model="approve">Approve</button>
</a>
而且,你的控制器中有一个错误。你把这个名字命名为#para;&#39; id&#39;和&#39; sts&#39;,然后你使用&#39; id&#39;和&#39;状态&#39;。