如何调用angularjs休息服务

时间:2015-01-05 13:22:41

标签: javascript angularjs

我是angularjs的新手,请帮助。

如何为我的代码获取java rest服务。我使用mockup json数据。

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;
});

1 个答案:

答案 0 :(得分:1)

你可以使用Spring来做到这一点。

基本上,您可以执行以下操作:

创建一个spring项目并创建一个文件(例如Greeting.java)

package hello;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

使用@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。

了解更多信息,请查看herehere