将mongoose item id传递给angularjs中的路由

时间:2015-10-02 11:09:31

标签: angularjs mongoose

我目前正在使用mongoose,它会为我的数据项自动生成id,这是以字母数字的形式  _id: "560e617a7f84101a6bf85c37"

我已将ui-router配置为

state('showallitems.edititem', {
    controller: 'ModalCtrl',
    url: "/editItem/:id",
    template: ""
  });

和html:

<a class="btn btn-primary"ui-sref="showallitems.edititem({id: {{item._id}} })">Edit item!</a>

问题是_id属性正在转换为INTEGER,它变得无限。我该如何避免这个问题?

1 个答案:

答案 0 :(得分:1)

我已经习惯了这种方式,它对我有用......

我的代码:

角度版&#34; ~1.3.15&#34;
angular-ui-router &#34; ~0.2.10&#34;
猫鼬版&#34; ~3.8.0&#34;
MongoDB shell版本: 2.4.14

路线

.state('main.installation.form', {
    url: '/form/:installationId',
    views: {
      'container@main': {
        templateUrl: 'app/installation/installation.form.html',
        controller: 'InstallationFormCtrl'
      }
    }
})

<强> list.html

<tr ng-repeat="installation in installations">
    <td class="col-md-2">{{installation.name}}</td>
    <td class="col-md-9">{{installation.description}}</td>
    <td class="col-md-1 text-center">
      <a class="cursor-pointer" title="Editar" ng-click="editInstallation(installation)"><i class="fa fa-pencil"></i></a>
    </td>
</tr>

<强> list.controller.js

$scope.installations = [];

installationFactory.listAll().get().$promise.then(function(response){
    $scope.installations = response;
});

$scope.editInstallation = function(installation) {
    $state.go('main.installation.form', { installationId: installation._id } );
};

<强> form.controller.js

$scope.installation = {};

if($stateParams.installationId){
    installationFactory.findById().get({installation: $stateParams.installationId}).$promise.then(function(response){
      $scope.installation = response;
    });
}

网址是: URL of my app with _id in route