在index.html中,我添加了控制器文件的路径。
我收到控制器的错误。即使我在controller.js中注释掉代码,我仍然会收到此错误。
controller.js
angular.module('starter.controllers', [])
.controller('AppCtrl', function($scope,$state,$stateParams,$rootScope){
});
app.js
// Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic', 'starter.controllers'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('navBar', {
url: "/navBar",
abstract: true,
templateUrl: "templates/navigationBar.html",
controller:'AppCtrl'
})
.state('navBar.orderReceive', {
url: "/orderReceive",
views: {
'orderReceiveView': {
templateUrl: "templates/OrderReceiveChoice.html"
}
}
})
$urlRouterProvider.otherwise(function ($injector) {
var $state = $injector.get('$state');
$state.go('navBar.orderReceive');
});
})
但是我收到了一个错误 错误:
TypeError: Cannot read property 'controller' of undefined
at IonicModule.controller.self.update (ionic.bundle.js:51695)
at IonicModule.controller.self.beforeEnter (ionic.bundle.js:52135)
at IonicModule.controller.self.beforeEnter (ionic.bundle.js:54079)
at Scope.$get.Scope.$emit (ionic.bundle.js:24919)
at Object.IonicModule.factory.ionicViewSwitcher.create.switcher.emit (ionic.bundle.js:50634)
at Object.IonicModule.factory.ionicViewSwitcher.create.switcher.transition (ionic.bundle.js:50492)
at ionic.bundle.js:52121
at ionic.bundle.js:50370
at Object.IonicModule.factory.ionicViewSwitcher.create.switcher.render (ionic.bundle.js:50459)
at Object.IonicModule.factory.ionicViewSwitcher.create.switcher.init (ionic.bundle.js:50369)(anonymous function) @ ionic.bundle.js:21157$get @ ionic.bundle.js:17936$get.Scope.$emit @ ionic.bundle.js:24921IonicModule.factory.ionicViewSwitcher.create.switcher.emit @ ionic.bundle.js:50634IonicModule.factory.ionicViewSwitcher.create.switcher.transition @ ionic.bundle.js:50492(anonymous function) @ ionic.bundle.js:52121(anonymous function) @ ionic.bundle.js:50370IonicModule.factory.ionicViewSwitcher.create.switcher.render @ ionic.bundle.js:50459IonicModule.factory.ionicViewSwitcher.create.switcher.init @ ionic.bundle.js:50369IonicModule.controller.self.render @ ionic.bundle.js:52115IonicModule.controller.self.register @ ionic.bundle.js:52073updateView @ ionic.bundle.js:57485IonicModule.directive.compile @ ionic.bundle.js:57469invokeLinkFn @ ionic.bundle.js:17477nodeLinkFn @ ionic.bundle.js:16977compositeLinkFn @ ionic.bundle.js:16368nodeLinkFn @ ionic.bundle.js:16972compositeLinkFn @ ionic.bundle.js:16368nodeLinkFn @ ionic.bundle.js:16972compositeLinkFn @ ionic.bundle.js:16368nodeLinkFn @ ionic.bundle.js:16972compositeLinkFn @ ionic.bundle.js:16368publicLinkFn @ ionic.bundle.js:16243IonicModule.controller.self.appendViewElement @ ionic.bundle.js:52259IonicModule.factory.ionicViewSwitcher.create.switcher.render @ ionic.bundle.js:50449IonicModule.factory.ionicViewSwitcher.create.switcher.init @ ionic.bundle.js:50369IonicModule.controller.self.render @ ionic.bundle.js:52115IonicModule.controller.self.register @ ionic.bundle.js:52073updateView @ ionic.bundle.js:57485(anonymous function) @ ionic.bundle.js:57462$get.Scope.$broadcast @ ionic.bundle.js:24992$state.transitionTo.$state.transition.resolved.then.$state.transition @ ionic.bundle.js:44836processQueue @ ionic.bundle.js:23394(anonymous function) @ ionic.bundle.js:23410$get.Scope.$eval @ ionic.bundle.js:24673$get.Scope.$digest @ ionic.bundle.js:24484$get.Scope.$apply @ ionic.bundle.js:24778done @ ionic.bundle.js:19191completeRequest @ ionic.bundle.js:19363requestLoaded @ ionic.bundle.js:19304
编辑:根据建议,我改为此,但我仍然得到同样的错误。事实上,如果我删除整个控制器部分,我仍然会在starter.controllers
中注入app.js
。
angular.module('starter.controllers', [])
.controller('AppCtrl', ['$scope', '$state','$stateParams','$rootScope', function($scope,$state,$stateParams,$rootScope){
$scope.data = [
{name: 'Giani, Pitampura', _id: '01', city:'Delhi'},
{name: 'Giani, Rajouri', _id: '02', city:'Delhi'},
{name: 'Dominoz, Mayur Vihar ', _id: '03', city:'Delhi'},
{name: 'Dominoz, Andheri east', _id: '04', city:'Mumbai'},
{name: 'Dominoz, Andheri west', _id: '05', city:'Mumbai'},
{name: 'Pizaa planet, west road', _id: '06', city:'Kolkata'},
{name: 'Food panda, Gurgaon', _id: '07', city:'Haryana'}
];
$scope.getOutlets=function(cityname){
console.log("called")
$scope.outlets = data.map(function(obj){
return obj.city === cityname
})
}
}]);
编辑2:
我刚刚意识到我因为嵌套状态而得到了这个。如果我使状态非抽象并附加控制器,它工作正常。如何保留嵌套状态而不会出现此问题?