Javascript AngularJS:can';找不到注册的控制器
我对angular还不熟悉,但已经创建了一个相当简单的应用程序。我看过其他类似的问题,但没有发现任何明显的拼写错误 在我的html文件中Javascript AngularJS:can';找不到注册的控制器,javascript,html,angularjs,Javascript,Html,Angularjs,我对angular还不熟悉,但已经创建了一个相当简单的应用程序。我看过其他类似的问题,但没有发现任何明显的拼写错误 在我的html文件中 <div ng-controller="OutcomeController" data-ng-init= "init()"> <div class="title"> <h1>Runs</h1> <br/> </div>
<div ng-controller="OutcomeController" data-ng-init= "init()">
<div class="title">
<h1>Runs</h1>
<br/>
</div>
<table width="98%">
<tr>
<td>
<div class="searchPlace">
<form ng-submit="showCprRequest()" class= "search-form">
<input type="text" class="search-input" ng-model="idText"
placeholder="Search Id" autofocus>
<button type="submit" class="search-submit" >Search</button>
</form>
</div>
</td>
</tr>
</table>
没有任何console.log被命中,我得到的错误是,
OutcomeController
未注册。但我这样做的底线正确吗?有什么明显的遗漏吗?因为您没有为角路由器添加ng应用程序和CDN。
我们可以使用CDN,也可以使用npm安装角路由。
控制台打印正确,您需要添加$scope.showDefaultCPRequest(),因为没有与此相关的函数。
您可以从这里找到所有CDN:
HTML:
<!doctype html>
<html ng-app="App">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular-route.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div ng-controller="OutcomeController" ng-init= "init()">
<div class="title">
<h1>Runs</h1>
<br/>
</div>
<table width="98%"><tr>
<td>
<div class="searchPlace">
<form ng-submit="showCprRequest()" class= "search-form">
<input type="text" class="search-input" ng-model="idText" placeholder="Search Id" autofocus>
<button type="submit" class="search-submit" >Search</button>
</form>
</div>
</td>
</tr></table>
</div>
</body>
</html>
var app = angular.module('App',['ngRoute']);
app.controller('OutcomeController', ['$scope', '$rootScope', '$location' ,'$http', '$log', '$routeParams', function ($scope, $rootScope, $location ,$http, $log, $routeParams)
{
$scope.init = function () {
// get id from url parameter (html head part)
console.log('here');
$scope.id=$routeParams.id;
// if id already entered on the page, get it from there
if ($scope.iIdText != null && $scope.idText != undefined && $scope.idText != "") {
$scope.showCprRequest();
}
else if ($scope.id == null || $scope.id == undefined) {
$scope.showDefaultCprRequest();
}
else {
$scope.iIdText = $scope.id;
$scope.showCprRequest();
}
$scope.showCprRequest = function () {
if ($scope.IdText.length == 0) {
console.log('or there');
return;
}
console.log('here');
var requestUrl = baseUrl+ "/cpr/" + $scope.id;
$http({method: "GET", url: requestUrl}).
success(function (data, status) {
$scope.diceRequestDisplays = data;
$scope.itemsPerPage = $scope.totalItems;
})
};
};
}]);
您能否尝试将outcouts.js中的最后一行替换为angular.module('outcout').controller('OutcomeController',OutcomeController);请让我们知道您在这次更改后的观察结果。与以前相同的问题,没有任何更改谢谢。在浏览器中加载页面后,能否通过屏幕截图,使用ng app和ng controller='OutcomeController'突出显示html元素。提前谢谢。我在代码的其他地方声明了这个应用程序,它可以正常工作
var app = angular.module('App',['ngRoute']);
app.controller('OutcomeController', ['$scope', '$rootScope', '$location' ,'$http', '$log', '$routeParams', function ($scope, $rootScope, $location ,$http, $log, $routeParams)
{
$scope.init = function () {
// get id from url parameter (html head part)
console.log('here');
$scope.id=$routeParams.id;
// if id already entered on the page, get it from there
if ($scope.iIdText != null && $scope.idText != undefined && $scope.idText != "") {
$scope.showCprRequest();
}
else if ($scope.id == null || $scope.id == undefined) {
$scope.showDefaultCprRequest();
}
else {
$scope.iIdText = $scope.id;
$scope.showCprRequest();
}
$scope.showCprRequest = function () {
if ($scope.IdText.length == 0) {
console.log('or there');
return;
}
console.log('here');
var requestUrl = baseUrl+ "/cpr/" + $scope.id;
$http({method: "GET", url: requestUrl}).
success(function (data, status) {
$scope.diceRequestDisplays = data;
$scope.itemsPerPage = $scope.totalItems;
})
};
};
}]);