我的AngularJS-app存在一些问题。我试图在stackoverflow等上找到一些解决方案。但我根本无法摆脱这个问题。我理解错误信息,但我无法在自己的代码中找到问题。
我的项目中有四个不同的控制器,其中两个像我想要的那样运行 - 其中两个没有。我粘贴了很好的代码(githubController),但krisController和eniroController根本不起作用......
请帮忙
提前致谢
app.js
(function(){
var app = angular.module("app", ["ngRoute"]);
app.config(function($routeProvider){
$routeProvider
.when("/index.html", {
templateUrl: "index.html",
controller: "searchForData"
})
.when("/github/:username", {
templateUrl: "templates/github.html",
controller: "githubController"
})
.when("/tv/:channel", {
templateUrl: "templates/tv.html",
controller: "tvController"
})
.when("/eniroSearch/:searchword", {
templateUrl: "templates/eniro.html",
controller: "eniroController"
})
.when("/krisinfo", {
templateUrl: "templates/krisInfo.html",
controller: "krisController"
})
.otherwise({redirectTo:"/index"});
});
}());
eniroController.js
(function() {
var module = angular.module("app"); //Referens till modul
var eniroController = function($scope, eniro, $routeParams){
var onEniroComplete = function(data){
$scope.eniro = data;
$scope.eniroArray = [];
for(var i = 0; i < $scope.eniro.adverts.length; i++){
$scope.eniroArray[i] = $scope.eniro.adverts[i];
}
};
var onError = function(reason) {
$scope.error = "Could not fetch data";
};
var eniroSearch = function(data){
eniro.eniroSearch($scope.searchWord).then(onEniroComplete,onError);
};
$scope.searchWord = $routeParams.searchword;
eniro.eniroSearch($scope.searchWord).then(onEniroComplete, onError);
};
module.controller("eniroController", eniroController);
}());
githubController
(function() {
var app = angular.module("app"); //Referens till modul
var githubController = function($scope, github, $routeParams){
var onGithubComplete = function(data){
$scope.user = data;
github.getRepos($scope.user).then(onGitHubRepos, onError);
};
var onGitHubRepos = function(data){
$scope.repos = data;
};
var onError = function(reason) {
$scope.error = "Could not fetch data";
};
$scope.username = $routeParams.username;
github.getUser($scope.username).then(onGithubComplete, onError);
};
app.controller("githubController", githubController);
}());
krisController
(function(){
var module = angular.module("app");
var krisController = function($scope, kris, $routeParams) {
var onFeedComplete = function(data){
$scope.kris = data;
$scope.krisArray = [];
for(var i = 0; i < $scope.kris.Entries.length; i++){
$scope.krisArray[i] = $scope.kris.Entries[i];
}
};
var onError = function(reason) {
$scope.error = "Could not fetch data";
};
var getFeed = function(data){
kris.getFeed.then(onFeedComplete, onError);
};
kris.getFeed.then(onFeedComplete, onError);
};
module.controller("krisController", krisController);
}());
错误:[ng:areq] http://errors.angularjs.org/1.2.5/ng/areq?p0=krisController&p1=not%20aNaNunction%2C%20got%20undefined 在错误(本机) 在https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js:6:449 at tb(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js:18:250) 在Qa(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js:18:337) 在https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js:61:212 在链接(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular-route.min.js:7:180) 在K(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js:49:142) 在f(https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js:42:172) 在https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js:41:337 在https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js:43:54
的index.html
<!DOCTYPE html>
<html lang="sv" ng-app="app">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title>LXSU Angular-Project</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular-route.min.js"></script>
<script src="js/app.js"></script>
<script src="js/eniroService.js"></script>
<script src="js/eniroController.js"></script>
<script src="js/githubService.js"></script>
<script src="js/githubController.js"></script>
<script src="js/tvController.js"></script>
<script src="js/tvService.js"></script>
<script src="js/search.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<!-- Custom styles for this template -->
<link href="style/dashboard.css" rel="stylesheet">
</head>
<body ng-controller="searchForData">
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">LXSU Angular-Project</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li><a href="#">#</a></li>
<li><a href="#">#</a></li>
<li><a href="#">#</a></li>
<li><a href="#">#</a></li>
</ul>
</div>
</div>
</nav>
<div class="container-fluid">
<div class="row">
<div class="col-sm-3 col-md-2 sidebar">
<ul class="nav nav-sidebar">
<li class="active"><a href="#"># <span class="sr-only">(current)</span></a></li>
<li><a href="#">#</a></li>
<li><a href="#">#</a></li>
<li><a href="#">#</a></li>
</ul>
<ul class="nav nav-sidebar">
<li><a href="">#</a></li>
<li><a href="">#</a></li>
</ul>
</div>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<h1 class="page-header">Search</h1>
<div class="input-group">
<form class="navbar-form navbar-right" name="searchUser" ng-submit="search(searchWord)">
<input type="text" class="form-control" placeholder="Search for..." ng-model="searchWord">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Go!</button>
</form>
</span>
</div><!-- /input-group -->
<h2 class="sub-header">{{ error }}</h2>
<div class="table-responsive" >
<table class="table table-striped" ng-view>
</table>
</div>
</div>
</div>
</div>
</body>
</html>
答案 0 :(得分:2)
您的HTML页面包含某些控制器的脚本,但并非适用于所有控制器。因此,他们没有角度。
缺少(至少):
<script src="js/krisController.js"></script>