I am unable to get the URL to work via my service even though I can get it to work in the browser.
我无法通过我的服务获取URL,即使我可以在浏览器中使用它。
The "$http()" doesn't seem to get invoked. The first alert() comes up, but that is it.
“$ http()”似乎没有被调用。第一个警报()出现了,但就是这样。
This is what come back in the browser for the URL = http://localhost:8080/myapp/students
这是在URL = http:// localhost:8080 / myapp / students的浏览器中返回的内容
[{"id":1,"name":"denis","courses":[{"id":1,"name":"mycourse"}]}]
Can someone make some suggestions as to what I am doing wrong?
有人可以就我做错了什么提出一些建议吗?
studentform.js:
"use strict";
angular.module('StudentApp',[]);
angular.module('StudentApp').service('StudentModel', [function($http){
this.students = [{name:"NONE"},{name:"NONE2"}];
this.getStudents = function(){
alert("getStudents called.");
$http({method: 'GET', url: 'http://localhost:8080/myapp/students'}).
then( function(data, status, headers, config){
alert("Success!!");
this.students = data;
}).
catch( function(data, status, headers, config){
alert("Failed!!");
console.log( "Error: " + status );
});
// $http.get('http://localhost:8080/myapp/students')
// .then( function (response) {
// this.students = response.data;
// }. bind( this), function (response) {
// console.log( response.data);
// });
// $http.get('http://localhost:8080/myapp/students')
// .then(function(response){
// alert( "in then");
// alert( response.date );
// this.students = response.data;
// }.bind(this),function(response){
// alert( "in bind" );
// console.log(response.data);
// });
// alert(response.data);
}
}]);
angular.module('StudentApp').controller('StudentController', ['StudentModel', function(StudentModel){
this.model = StudentModel;
}]);
studentform.jsp
<!DOCTYPE html>
<html ng-app="StudentApp">
<head>
<meta charset="UTF-8">
<script src="/myapp/resources/js/angular/angular.js"></script>
<script src="/myapp/resources/js/studentform.js"></script>
<title>Students</title>
</head>
<body ng-controller="StudentController as main">
<div>
<p><button ng-click="main.model.getStudents()">Get Students</button></p>
<ul>
<li ng-repeat="student in main.model.students">
{{student.name}}
</li>
</ul>
</div>
</body>
</html>
1 个解决方案
#1
0
You forgot to add $http
as a dependency
你忘了添加$ http作为依赖
Change your service definition to below code:
将您的服务定义更改为以下代码:
angular.module('StudentApp').service('StudentModel', ['$http', function($http){
#1
0
You forgot to add $http
as a dependency
你忘了添加$ http作为依赖
Change your service definition to below code:
将您的服务定义更改为以下代码:
angular.module('StudentApp').service('StudentModel', ['$http', function($http){