如何将angularjs服务中的数据发送到操作结果

时间:2016-04-19 13:07:53

标签: angularjs asp.net-mvc

我有这项服务

template<class T>
constexpr T Pi()
{
    using is_fp = std::is_floating_point<T>;
    static_assert( is_fp::value, "Pi must be instantiated with floating point types.");
    return T{3.1415926535897932384626433832795};
}

这是我的控制器

  this.getyear = function (bookId) {
            alert();
            var response = $http({
                method: "post",
                url: "Dashboard/GetyearId",
                data: JSON.stringify(12,14),
                dataType: "json"
            });
            return response;
        }

这是我的actionresult方法

controller('myController', function ($scope, AttendanceService) {
  var getyear = AttendanceService.getyear();
            getyear.then(function (_book) {

            })
});

我想将2-3个ID传递给actionresult并在那里进行检索。它怎么可能?我是angularJS的新手。提前谢谢

2 个答案:

答案 0 :(得分:1)

我认为service可能是这样的:

//service
this.getyear = function (bookId) {

    return $http({
        method: "post",
        url: "Dashboard/GetyearId",
        data: JSON.stringify([12, 13, 14]) //for three ids
    });
}

//and controller:
controller('myController', function ($scope, AttendanceService) {
    AttendanceService.getyear()
        .then(function (_book) {

        })
});

答案 1 :(得分:0)

我更喜欢这样打电话 -

this.getyear = function (bookId) {
     var url =  "Dashboard/GetyearId",
     var data =  [12,10]; //A json object
            //or
     var data =  JSON.stringify([10,14]) //in your case, but better pass and receive objects

     return $http.post(url, data);
}