$ state is params对于我的angular js项目是空的 [英] $state is params is empty for my angular js project
本文介绍了$ state is params对于我的angular js项目是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是angular的新手,所以我使用$ state更改视图状态.我的状态代码是
I am new in angular.So i am using $state for changing the view state.My state code is
angular.module('myApp.controller', []).controller('firstController', ['$scope', '$state', function($scope, $state) {
$scope.loadView2 = function() {
$state.go('secondView', {
firstname: $scope.firstname,
lastname: $scope.lastname
});
};
}]);
angular.module('myApp.controller').controller('secondController',
function($scope, $stateParams, $state) {
console.log($state.params); // empty object
$scope.firstname = $stateParams.firstname; // empty
$scope.lastname = $stateParams.lastname; //empty
});
我的路线在跟踪
angular.module('myApp', ['myApp.controller', 'ui.router']).config(['$stateProvider', function($stateProvider) {
$stateProvider.state('firstView', {
url: '/fisrt-view',
templateUrl: './partials/view1.html',
controller: 'firstController'
});
$stateProvider.state('secondView', {
url: '/second-view',
templateUrl: './partials/view2.html',
controller: 'secondController'
});
但是我总是在view2中得到空白对象.
But i always get blank object in view2.
推荐答案
要在视图之间传递数据,您必须在第二个视图中定义参数,例如:
to pass data between views you have to define params in your second view, like this:
$stateProvider.state('secondView', {
url: '/second-view',
templateUrl: './partials/view2.html',
controller: 'secondController',
params: {
mynewparam: null
}
});
然后,当您切换视图时:
then when you switch view:
$state.go('secondView', {mynewparam: 'newParam'});
,您将在视图控制器中检索数据,如下所示:
and you retrive the data inside the view controller like this:
$scope.mynewparam = $stateParams.mynewparam;
还请注意,无需重复$ stateProvider即可链接状态,例如:
notice also that you can chain states without repeating $stateProvider, example here:
$stateProvider
.state('firstView', {
url: '/fisrt-view',
templateUrl: './partials/view1.html',
controller: 'firstController'
})
.state('secondView', {
url: '/second-view',
templateUrl: './partials/view2.html',
controller: 'secondController',
params: {
mynewparam: null
}
});
这篇关于$ state is params对于我的angular js项目是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文