数据不在angular1服务中传递我使用$ routeparams

时间:2017-05-13 17:10:15

标签: angularjs

我关注角度为ui-router且依赖angular-ui-router/1.0.3/

角度依赖1.2.x

angular.module('todoservice', [])
.factory('user', ['$http','$stateParams', function($http, $stateParams){          
     userDetails: function(id){                           
    //:id is working when I pass static Id which is present in mongodb 
          return $http.get('/userDetails/display/:id', id);
//here is the problem i dont know how to pass id with angular to express(api is working perfectly)/
         },

这是我的角度控制器

user.userDetails(id).success(function(data){ console.log("data from services to controller", data); $scope.userDetails_test = data; });

这是我的角度配置文件

angular.module('app',  ['todoController', 'todoservice','ui.router']).
  config(['$stateProvider', function($stateProvider) {
          $stateProvider.state('userDetails', {
            url: '/userDetails/:id',
                controller:'maincontroller',
                 params: {id: null,}
      }); 
  }]);

这是我的观点

//todo._id==> working perfectly with getting an id in the table with ng-repeat

<a ui-sref="userDetails({id: todo._id})" ng-href = "userDetails/:todo._id" ng-click = "userDetails_scope()">userDetails</a>

2 个答案:

答案 0 :(得分:0)

由于您尝试使用id patameter从服务器获取数据,因此可以使用$http.post,请参阅文档here

示例代码:

$http.post('/userDetails/display', {id: id})
     .then(function(response) {        // <------mention: here we are using then(success has been deprecated)
       // deal with response result from server here
     })

答案 1 :(得分:0)

你可以尝试将id传递给ng-click,如下所示 -

<a id ={id: todo._id} ui-sref="userDetails({id: todo._id})" ng-href = "userDetails/:todo._id" ng-click = "userDetails_scope(todo._id)">userDetails</a>