【问题标题】:I am not getting data while working with angular with web api by seperate service通过单独的服务使用 Web api 使用 Angular 时,我没有获取数据
【发布时间】:2017-05-25 10:40:25
【问题描述】:

它是services.js 文件,它正在从服务器获取数据。我有一个主脚本文件,虽然没有将数据输入到主 java 脚本方法表单服务层。

块引用

请检查代码,如果我有任何错误,请告诉我:

app.factory("studentServices", function ($http)## 



----------


 ##
 return {

        postData: function (student) {

            $http.post("/api/Student/Register", student)
              .then(function (response) {
                console.info(response.data);

                debugger;
                return response.data;

            }, function (error) {

                console.info("error in reruest");
            });
        },
        getData: function () {
            debugger;
            var rt;
            $http.get("/api/Student/getStudent").then(function (response) {
                console.info(response.data);
                debugger;
                rt=response.data;
            }, function (error) { console.info("error in geting"); })
            debugger;
            return rt;
        }

        }
    })

“这是工厂的其他服务”

/// <reference path="../Scripts/angular.js" />
/// <reference path="script.js" />
var app = angular.module("myModule", []).controller("myController", function ($scope, $http, studentServices) {
    debugger;
    console.info("init");

    $scope.addStudent = function () {
        console.info("just");
        console.info($scope.id);
        $scope.result="";
        var student = {id:$scope.id,name:$scope.name,email:$scope.email,college:$scope.college,mobile:$scope.mobile}
        console.info(student);
      //  $http.post("/api/Student/Register", student).then(function (response) {
        //    console.info(response.data);
        $scope.result = studentServices.postData(student);
            console.info("result " + $scope.result);
            get();
       // }, function (error) { console.info("error in reruest") })
        //$scope.getStudents();
    }


    $scope.getStudents = function get() {
        debugger;
            console.info("gettinf studetn list");
        //    $http.get("/api/Student/getStudent").then(function (response) {
        //      console.info(response.data);
            var local = studentServices.getData();
            debugger;
            $scope.students = local;  //response.data;
            debugger;
            console.info("getdata list  ");

    //        }, function (error) { console.info("error in geting") })

        }
        console.info("last");

})

【问题讨论】:

  • 你在哪里调用你的工厂方法?

标签: angularjs asp.net-web-api


【解决方案1】:

您需要更正以下内容:

1.在您的工厂方法中,只需返回 api 承诺。例如:

postData: function (student) { 
    return  $http.post("/api/Student/Register", student);
}

2.在您的控制器中使用工厂方法及其响应: 例如:

studentServices.postData(student).then(function (result){    
    $scope.result = result.data;
}, function(error){
    // handle error
})

getData 执行类似操作。

由于$http 是异步的,你必须在控制器中解析数据,因此获取数据。看到这个fiddle

【讨论】:

  • 为什么我们需要返回它我们在函数内部重新调整
  • @teja:因为$http is asynchronous,$scope 在调用完成之前正在接受数据。这是空的,。所以 $scope 中没有数据。您必须在控制器中解决它才能分配数据。
猜你喜欢
  • 1970-01-01
  • 2018-07-22
  • 2013-08-24
  • 2016-11-08
  • 2020-05-25
  • 2018-02-20
  • 2018-08-25
  • 2020-01-15
  • 2015-09-06
相关资源
最近更新 更多