我从这两种方法中得到了意想不到的结果。
我的$ state configed
$stateProvider
.state('status', {
url: "/status/:payment",
controller: 'QuestCtrl',
templateUrl: "index.html"
});
在控制器上我有:
angular.module('quest').controller('QuestCtrl',function($scope,$stateParams,$state){
console.log($stateParams.payment); // undefined
console.log($state); // Object {params: Object, current: Object, $current: extend, transition: null}
}
我已经在其他项目中使用了$ stateParams并且它有效但现在我无法弄清楚这里发生了什么......
答案 0 :(得分:4)
['$scope','$stateParams','$state',
function($scope, $http, $stateParams, $state)
服务名称与变量不匹配。
所以$ http实际上是$ stateParams服务,$ stateParams实际上是$ state服务,$ state是未定义的。
我的建议:停止使用此数组表示法,这会使代码混乱,并且是错误的常见来源。相反,使用ng-annotate作为构建过程的一部分,它将正确地为您完成。
答案 1 :(得分:4)
As I already commented above You forgot to inject $http service
angular.module('quest').controller('QuestCtrl',
['$scope','$http','$stateParams','$state',function($scope,$http,$stateParams,$state){
console.log($stateParams); // Empty Object
console.log($stateParams.payment); // Empty Object
console.log($state); // I get the entire state, I can see that my params are there.
console.log($state.params);
}
So your parameters mismatch and it turns out you will get $state in $stateparms and $state is empty. And $http hold $state :P
Hope it helps :)
答案 2 :(得分:1)
With the ng-annotate library, the controller can be also initiated like this:
angular.module('quest')
.controller('QuestCtrl', function ($scope,$http,$stateParams,$state) {
});
In this case you are avoiding problems with the injected objects ordering. Look at: https://github.com/olov/ng-annotate
If you are building your application with Grunt, use: grunt-ng-annotate
package.
答案 3 :(得分:0)
调用'ngInject' 构造函数($ scope,$ reactive,$ stateParams,$ state,$ sce){ 'ngInject'
答案 4 :(得分:0)
routes.js中缺少参数
我的例子:
.state('menu.cadastroDisplay', {
url: '/page9',
views: {
'side-menu21': {
templateUrl: 'templates/cadastroDisplay.html',
controller: 'cadastroDisplayCtrl'
}
},
params: { 'display': {} }
})
如果路径中没有此参数,$ stateParams.yourParam将始终返回undefined。