2015-01-05 158 views
-2

我是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; 
}); 
+2

你能澄清这个问题吗? –

回答

1

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

Basicly你可以做到以下几点:

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

并运行应用程序

​​

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

+0

欢迎来到[so]!请尽可能避免发布指向其他网页的答案。如果源代码很有用,请在你的答案中引用它的相关部分,或者更好地用你自己的话来重写它(当然,你仍然可以引用它)。有关更多信息,请参阅[答案]。谢谢! –

+0

@ Qantas94Heavy这样更好吗? – joey