2015-11-09 42 views
0

这一个是我的服务:POST HTTP://本地主机:53178/API/LocationSearchApi 404(未找到)

latModule.service('latLocationSvc', 
     ["$http", "$resource", 
      function ($http, $resource) { 
    this.post = function (params) { 
        var request = $http({ 
          method: "post", 
          url: "/api/LocationSearchApi", 
          data: params 
         }); 
         return request; 
        }; 
    }]); 

这一个是我的控制器:

 $scope.searchFilterData = function() { 
      var params = { 
       SponsorIcaCid: 88150, 
       OwnerIcaCid: null, 
       Cid: null, 
       TerminalId: null, 
       RtnCode: null, 
       LocationName: null, 
       LocationTypeCode: null, 
       OwnerName: null, 
       CountryCode: null, 
       Address: null, 
       City: null, 
       StateProv: null, 
       Postal: null, 
       FromLastUpdateDate: null, 
       ToLastUpdateDate: null, 
       ErrorCode: null, 
       ViewFilterType: 5, 
       IsExportRequest: null, 
       ExportLocations: null, 
       MerchantName: null, 
       MerchantCategory: null, 
       MccCode: null, 
       DataProviderType: null, 
       DataProviderId: null, 
       PhoneNumber: null, 
       OptedOut: null 

      }; 
      var promisePost = latLocationSvc.post(params); 
      promisePost.then(function(response) { 
       $scope.locationDtlsList = response; 
      },function(){}); 

     }; 

在呼吁这个帖子API我收到以下错误。

POST http://localhost:53178/api/LocationSearchApi 404 (Not Found) 

我需要做什么来克服这个错误?当我通过使用$ resource实现服务来调用API时,它可以工作,但在呼叫完成之前,它会在数据到达之后触发事件。由于这个原因,我无法将数据绑定到我的视图。所以我正试图实现这一点。

+0

你可以使用$ timeout [link](https://docs.angularjs。组织/ API/NG /服务/ $超时) –

回答

1

如果您在查找数据后查找并调用事件,我列出了处理它的两种不同方法。

1)使用$viewContentLoaded

$viewContentLoaded事件被发出,这意味着获得这个事件中,你需要像父控制器

HTML:

<div ng-controller="MainCtrl"> 
    <div ng-view></div> 
</div> 

从MainCtrl你可以听事件

JS:

app.controller('MainCtrl', function ($scope, $timeout) { 
    $scope.$on('$viewContentLoaded', function(event) { 
     $timeout(function() { 
      //Here your view content is fully loaded !! 
     },0); 
    }); 
}); 

发出的每一道的ngView内容被重新装载时间。据$viewContentLoaded文件,它应该工作

2)使用directive

JS:

.directive('elemReady', function($parse) { 
    return { 
     restrict: 'A', 
     link: function($scope, elem, attrs) {  
      elem.ready(function(){ 
      $scope.$apply(function(){ 
       var func = $parse(attrs.elemReady); 
       func($scope); 
      }) 
      }) 
     } 
    } 
}); 

$scope.callMethod = function(){ 
    //Here your view content is fully loaded !! 
} 

HTML:

<div elem-ready="callMethod()"></div> 

我会ARG ue这是推荐的Angular方式,因为这样做的主要好处是您可以像您喜欢的那样使用广泛的或精细的UI,并且您正在使用控制器中的DOM逻辑。

让我知道是否有任何疑虑/查询此。

+0

其实当我尝试使用$资源VAR OBJ = $资源(的baseUrl, { 过滤器通过服务来实现这一点:参数,可以 用户名:用户id, serviceTypeCode:serviceTypeCode },{ 后 :{ 方法: 'POST', PARAMS:{}, IsArray的:假 } }); obj.post({},功能(响应){ vm.locationDtls =响应; 回调(响应); –

+0

和在控制器latLocationSvc.getLocationList(函数(结果){$ = scope.locationDtlsList结果;});其余的调用get完成,但是当我尝试在retrived对象上使用ng-repeat时,它不是wotking,因为在控制器得到响应之前ng-repeat会被执行。 –

+0

我认为你需要''timeout' $ scope.locationDtlsList = response;'你试过这个吗? –

相关问题