Javascript 如何调用angularjs rest服务
我是新来的,请帮忙 如何为我的代码获取java rest服务。我使用模拟json数据Javascript 如何调用angularjs rest服务,javascript,angularjs,Javascript,Angularjs,我是新来的,请帮忙 如何为我的代码获取java rest服务。我使用模拟json数据 var orderVariable = angular.module('ordermodule',[]); orderVariable.controller('formController',['$scope', '$state', function($scope, $state) { $scope.processForm = function() { alert('awesome!
var orderVariable = angular.module('ordermodule',[]);
orderVariable.controller('formController',['$scope', '$state', function($scope, $state) {
$scope.processForm = function() {
alert('awesome!');
};
$scope.formData = {};
}]);
///////////////////////////////////////////////////////
orderVariable.factory('orderableItemsService', function() {
var orderableItemsService = {
products:[{"name":"ACUVUE OASIS with HYDRACLEAR 6 Pack","description":{"description":"Acuvue Advance Plus contact lenses give your eyes what they need all day longsuperior comfort and moisture. Your eyes will retain the smooth sensation of a new pair of contacts thanks in large part to HYDRACLEAR technology, which builds moisture into the lens itself. Each time you blink, the built-in moisture acts as a natural lubricant for your eye. It's a refreshingly comfortable experience in a bi-weekly lens. This is the Acuvue Advance Plus 24 pack of contacts. You can also purchase this lens in a 6 pack.","details":"Details:1>LENS TYPE: 1-2 week soft disposable contact lenses.2>PACKAGE DETAILS: 24 lenses in buffered saline with methyl ehter cellulose.3>MATERIAL AND % OF CONTENT: 53% galyfilcon A.4>WATER % OF CONTENT: 47%.5>MANUFACTURER: Johnson and Johnson Vision Products, Inc., Jacksonville, FL",
"eyeType":"LeftOD","power":"-3.21","bc":"4","dia":"11.2","boxes":[1,2,3,4],"lensType":"1-2 week soft disposable contact lenses."},
"quantity":4,"itemid":"1","shippingaddress":"Library 1400 Chicago Ave Albany NY 12222","upc":124,"unitcost":160},
{"name":"ACUVUE OASIS with HYDRACLEAR 6 Pack","description":{"description":"Acuvue Advance Plus contact lenses give your eyes what they need all day longsuperior comfort and moisture. Your eyes will retain the smooth sensation of a new pair of contacts thanks in large part to HYDRACLEAR technology, which builds moisture into the lens itself. Each time you blink, the built-in moisture acts as a natural lubricant for your eye. It's a refreshingly comfortable experience in a bi-weekly lens. This is the Acuvue Advance Plus 24 pack of contacts. You can also purchase this lens in a 6 pack.","details":"Details:1>LENS TYPE: 1-2 week soft disposable contact lenses.2>PACKAGE DETAILS: 24 lenses in buffered saline with methyl ehter cellulose.3>MATERIAL AND % OF CONTENT: 53% galyfilcon A.4>WATER % OF CONTENT: 47%.5>MANUFACTURER: Johnson and Johnson Vision Products, Inc., Jacksonville, FL",
"eyeType":"RightOD","power":"-1.11","bc":"2","dia":"13.2","boxes":[1,2,3,4],"lensType":"1-2 week soft disposable contact lenses."},"quantity":4,"itemid":"2","shippingaddress":"Library 1400 Chicago Ave Albany NY 12222","upc":124,"unitcost":160},
{"name":"ACUVUE LENS SOLUTION","description":{"description":"Acuvue Advance Contact Lens Solution.........","details":"","eyeType":"","power":"","bc":"","dia":"","boxes":[1,2,3,4],"lensType":"Contact Lens Solution........."},"quantity":null,"itemid":"3","shippingaddress":null,"upc":null,"unitcost":null}],
checks: [],
checkr: []
};
return orderableItemsService;
});
orderVariable.controller('prodCtrl1', function($scope,$http,orderableItemsService) {
$scope.boxVal=0;
$scope.formData = orderableItemsService;
$scope.itemList=[];
});
orderVariable.controller('prodCtrl2', function($scope, orderableItemsService) {
$scope.formData = orderableItemsService;
});
您可以使用Spring来实现这一点 基本上,您可以执行以下操作: 创建一个spring项目并创建一个文件(例如Greeting.java) 使用@RestController获取所需的数据
package hello;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}
并使用
package hello;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
nog在角度控制器中获取日期,如下所示:
function Hello($scope, $http) {
$http.get('http://rest-service.guides.spring.io/greeting').
success(function(data) {
$scope.greeting = data;
});
}
用正确的地址替换rest-service.guides.spring.io等
欲了解更多信息,请查看和能否澄清问题?欢迎访问!在可能的情况下,请避免发布仅指向其他网页的答案。如果来源有用,在你的答案中引用相关部分,或者更好的是,用你自己的话重写它(当然,你仍然可以引用它)。有关详细信息,请参阅。谢谢@澳洲航空公司:这样更好吗?
function Hello($scope, $http) {
$http.get('http://rest-service.guides.spring.io/greeting').
success(function(data) {
$scope.greeting = data;
});
}