【问题标题】:Passing parameters from view to another view with another controller使用另一个控制器将参数从视图传递到另一个视图
【发布时间】:2015-08-21 15:33:58
【问题描述】:

我正在尝试调用 REST API,将两个参数传递给另一个视图和另一个控制器。

语法是/api/:id/something/:id2/something

在接收端,我有一个工厂来处理调用并将其传递给新页面的控制器。

这是我的观点:

<div>
    <table  class="table table-striped">
        <thead>
        <th style="padding-left: 20px;">ACN Name</th>
        <th>Product Name</th>
        <th>ContractNumber</th>
        <th>Start Date</th>
        <th>End Date</th>
        <th></th>
        <tr ng-repeat="row in acns">
            <td style="padding-left: 20px;"><a href="" ng-click="viewOrgList(ACNID)">{{row.ACNName}}</a></td>
            <td>{{row.ProductName}}</td>
            <td>{{row.ContractNumber}}</td>
            <td>{{row.ContractStartDate}}</td>
            <td>{{row.ContractEndDate}}</td>
            <td><a href="" ng-click="remove(row)"><i class="fa fa-times" style="font-size:1.5em;"></i></a></td>
        </tr>
    </table>
</div>

此视图的控制器如下所示:

app.controller('acnController', ['$scope','$location', '$routeParams', 
 function ($scope, $location, $routeParams) {

    //Passing acnId to networkOrg template to display.
    $scope.viewOrgList = function (acnId) {
        $location.path('/NetworkOrgs' + acnId);
    }

}]);

在 /NetworkOrgs 模板的控制器上,它看起来像这样:

app.controller('orgController', ['$scope', '$routeParams','$location', 'orgFactory', function ($scope, $routeParams, $locatoin, orgFactory) {
    //Capture routeParam id and display associated orgs
    $scope.org = $routeParams.acnId;
}]);



app.factory('orgFactory', ['$resource','$routeParams', function ($resource, $routeParams) {
    return $resource("http://localhost:16047/api/acn/:acnId/networkOrgs", { acnId: $routeParams.acnId }, {
        getOrgs: { method: 'GET', params: { acnId: '@acnId'} }
    })
}])

这对我不起作用,我得到一个“错误的论点”结果。

任何帮助都会很棒!

【问题讨论】:

    标签: javascript angularjs asynchronous angularjs-resource


    【解决方案1】:

    我敢打赌,您在视图中错误地传递了参数。

    拥有

    ng-click="viewOrgList(ACNID)"
    

    应该有

    ng-click="viewOrgList(row.ACNID)"
    

    【讨论】:

      【解决方案2】:

      您应该使用ng-click="viewOrgList(row.ACNID)" 而不是ng-click="viewOrgList(ACNID)"

      应该

      $scope.viewOrgList = function (acnId) {
          $location.path('/NetworkOrgs/:' + acnId);
      }
      

      而不是

      $scope.viewOrgList = function (acnId) {
          $location.path('/NetworkOrgs' + acnId);
      }
      

      这样试试

      途中:

      $routeProvider.when('/NetworkOrgs/:acnId', { 
            templateUrl: 'Pages/acn/partials/network_orgs.html', 
            controller: 'orgController' 
       }); 
      

      在控制器中:

      app.controller('orgController', ['$scope', '$routeParams','$location', 'orgFactory', function ($scope, $routeParams, $locatoin, orgFactory) {
          //Capture routeParam id and display associated orgs
          $scope.acnId= $routeParams.acnId; // ensure that you have acnId
           orgFactory.getOrgs($scope.acnId)
            .$promise.then(function(response) {
               console.log(response);// check response by console
             });
      }]);
      

      在 orgFactory 中:

      app.factory('orgFactory', ['$resource', function ($resource) {
          return {
                  getOrgs: function(acnId) {
                      var getOrgsById = $resource('http://localhost:16047/api/acn/:acnId/networkOrgs', {acnId: '@acnId'}, {
                              'get': {method: 'GET'} // if you expect return array from server then use 'get': {method: 'GET', isArray: true}
                          });
                       return getOrgsById .get({acnId: acnId});
                  };
      }]);
      

      【讨论】:

      • 厌倦了,但我仍然收到错误的请求。在我正在调用的模板的接收控制器上,我正在使用 $resource 服务。也许这里出了点问题。我正在使用 $routeParams 来捕获正在传递的 id: return $resource("localhost:16047/api/acn/:acnId/networkOrgs", { acnId: $routeParams.acnId }, { getOrgs: { method: 'GET', params: { acnId: '@ acnId'} } })
      • 首先你确保你在orgFactory中拥有$routeParams.acnId的值
      • 我的假设是 $routeParams.acnId 是从 $route 请求中提取的。我会在上面的内容中做出什么改变?
      • 您可以尝试console.log($routeParams.acnId); 显示acnId,您可以尝试$route.current.params。检查console.log($route.current.params);你得到了什么?
      • 我真的很感激大家的帮助。 shaishab,我已经实现了你的建议,但现在路由不起作用: //Network Orgs Routes $routeProvider.when('/NetworkOrgs/', { templateUrl: 'Pages/acn/partials/network_orgs.html',控制器:'networkOrgController' });
      猜你喜欢
      • 1970-01-01
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      相关资源
      最近更新 更多