Angularjs 中 ui-sref 和 $state.go 如何傳遞單個多個引數和將物件作為引數
阿新 • • 發佈:2019-01-10
一: 如何傳遞單個引數
首先,要在目標頁面定義接受的引數:
傳參,
ui-sref:
$state.Go:
接收引數,
在目標頁面的controller裡注入
二:傳遞多個引數其實也很簡單可以在上面的單個後面直接拼
1:目標頁面定義需要傳的傳輸個數
.state("default.certquery", {
url: "/certquery",
controller: "certQueryCtrl" ,
params:{'gymListIds':null,"name":null}, //這個是重點其他可以不用看,具體路由對應自己專案
template: "<div ui-view></div>"
})
2:controll裡面接受(注入
var params={'gymListIds':classId,"name":name};
$state.go('sureOrders',{myParam:JSON.stringify(params )});//跳轉頁面並傳值
我這裡是用頁面繫結的值作為引數傳過去,大家可以傳固定的,具體業務具體對待
三:傳遞物件
$stateProvider
.state('app.example1', {
url: '/example',
views: {
'menuContent': {
templateUrl: 'templates/example.html',
controller: 'ExampleCtrl'
}
}
})
.state('app.example2', {
url: '/example2/:object',
views: {
'menuContent': {
templateUrl: 'templates/example2.html',
controller: 'Example2Ctrl'
}
}
})
2)
.controller('ExampleCtrl', function ($state, $scope, UserService) {
$scope.goExample2 = function (obj) {
$state.go("app.example2", {object: JSON.stringify(obj)});
}
})
.controller('Example2Ctrl', function ($state, $scope, $stateParams) {
console.log(JSON.parse($state.params.object));
})