嗨,我是非常新的 Angulay js Ionic 开发。我尝试在单击按钮时进行基本导航。但是当我单击 Firebug 控制台中的按钮时,我收到此错误。我想在点击后导航到搜索页面
错误:$state 未定义
这是我的 App.js 代码
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('signin', {
url: "/sign-in",
templateUrl: "templates/sign-in.html",
controller: 'AppCtrl'
})
.state('app', {
url: "/app",
abstract: true,
templateUrl: "templates/menu.html",
controller: 'AppCtrl'
})
.state('app.search', {
url: "/search",
views: {
'menuContent': {
templateUrl: "templates/search.html"
}
}
})
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/sign-in');
});
这是我的 Controller
angular.module('starter.controllers', [])
.controller('AppCtrl', function($scope, $ionicPopup, $timeout, $ionicModal) {
$scope.signIn = function(user) {
//console.log('Sign-In', user);
$state.go('app.search');
};
})
我的 HTML 按钮是
<button class="button button-block button-positive" id="login-btn" ng-click="signIn(user)" type="submit">Log in</button>
最佳答案
您还应该养成正确注入(inject)依赖项的习惯。它应该看起来像这样(包括 $state 注入(inject)):
.controller('AppCtrl',['$scope', '$ionicPopup', '$timeout', '$ionicModal', '$state', function($scope, $ionicPopup, $timeout, $ionicModal, $state) {
$scope.signIn = function(user) {
//console.log('Sign-In', user);
$state.go('app.search');
};
}])
只需确保引号中的注入(inject)顺序与以下函数中的参数顺序相同。
关于javascript - $state.go 不工作 - 错误 : $state isnot defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28464056/