【问题标题】:how to send data into angular function using spring [duplicate]如何使用弹簧将数据发送到角度函数[重复]
【发布时间】:2015-06-23 14:41:43
【问题描述】:

我使用 spring 和 hibernate 与 angularjs.. 我正在从数据库中获取数据,但我想在 angular 函数中获取我的数据..

这是我的角度函数

   artle = angular.module('myApp', []);

   artle.controller('myCtrl', function($scope) {
       $scope.class = "div0f";
       $scope.changeClass = function() {
           if ($scope.class === "div0f")
               $scope.class = "div0p";
           else
               $scope.class = "div0f";
       };
       $scope.Unsub = function() {
           if ($scope.class === "div0p")
               $scope.class = "div0g";
       };
   })

我想从数据库中获取一个值,以便在 if else 条件下在此函数中使用。

请给我建议。 提前谢谢

【问题讨论】:

    标签: javascript java angularjs spring


    【解决方案1】:

    正如 BetaRide 所说,您将在 JAX 调用的回调中执行此操作。在 Angular 中,您将要求对 Spring REST Endpoint 执行一些操作。当 Spring 完成时,它会向您发送答案。如果回答成功则进入成功回调方法,如果有错误则返回错误。

    $http({
        url: "http://example.com/rest/test",
        method: "POST",
        data: {"toto":"titi"}
    }).success(function(data, status, headers, config) {
        $scope.data = data; // Here you can call your method with data
    }).error(function(data, status, headers, config) {
        $scope.status = status; // Managae error
    });
    

    你必须管理你的 $http 注入

    angular.module('myApp.controllers', []).
    controller('ImagesCtrl', ['$scope', '$http', function ($scope, $http) {
      // ...
    });
    

    【讨论】:

    • REST 是如何构建 Web 服务的规范。请参阅this for a description。在 Java 中,它指的是 JAX-RS 规范,通常在 Spring with Jersey 中使用。
    • 所以在我的视图中使用 angular js 时,我应该使用 Rest 吗?还是有其他替代 api 的方法?
    • 让我们回到开头。您的应用程序中有两个部分:正面和背面。后面是带有数据库的弹簧,前面是你的角度。您希望 Angular 和 Spring 进行通信。沟通有几个规范。休息是有角度的(建议)。要向 Spring 请求一些数据,您需要通过 Webservices 上的 Ajax 调用执行 HTTP 请求。然后,Spring 将向您发送回将由 Angular 管理的数据。请参考this for a example of architecture
    • 如果我想从数据库中删除一条记录,那么我是否需要在角度函数中传递该记录的 id 或者可以通过不传递 id 来完成?因为我不想在我的 html 代码中做任何工作。
    • 感谢您的建议
    猜你喜欢
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 2018-11-12
    • 2020-09-17
    • 1970-01-01
    相关资源
    最近更新 更多