例如,有一个状态,它控制2个视图:
WWW / index.html中
<html>
.
.
.
<body ng-app='starter' ng-controller="MainCtrl"><p>
<ui-view name="redView" style="color:red;"></ui-view>
<ui-view name="greenView" style="color:green;"></ui-view>
</body>
</html>
WWW / JS / app.js
angular.module('starter',['ionic'])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('contacts',{
'views':{
'redView':{
template:'<p>Red View</p>',
controller:function($scope,$stateParams){
console.log($stateParams.redString);
}
},
'greenView':{
template:'<p>Green View</p>',
controller:function($scope,$stateParams){
console.log($stateParams.greenString);
}
}
}
});
})
.controller('MainCtrl', function($scope,$state) {
$state.go('contacts',{redString:'abc',greenString:'def'});
});
我传递参数&#34; redString&#34;和&#34; greenString&#34;到州&#39;联系人&#39;,但
console.log($stateParams.redString);
和
console.log($stateParams.greenString);
打印undefined。我究竟做错了什么?怎么让
console.log($stateParams.redString);
打印&#39; abc&#39;成功?
答案 0 :(得分:0)
您必须为状态对象
中的参数声明默认值.state('contacts',{
'views':{
'redView':{
template:'<p>Red View</p>',
controller:function($scope,$stateParams){
console.log($stateParams.redString);
}
},
'greenView':{
template:'<p>Green View</p>',
controller:function($scope,$stateParams){
console.log($stateParams.greenString);
}
},
params: {
//here
redString: null,
greenString: null
}
})
.controller('MainCtrl', function($scope,$state) {
$state.go('contacts',{redString:'abc',greenString:'def'});
});