簡體   English   中英

使用$ routeparams時出現“ SyntaxError:意外令牌<”

[英]“SyntaxError: Unexpected token <” when using $routeparams

使用mongodb,express和nodejs開發一個角度應用程序。 在應用程序中,我有一個單位列表,並希望為每個單位添加“編輯”選項。 這是我的api.js的一部分:

api.route('/flats/:id')
  .get(function(req, res){
    Flat.findById(req.params.id, function(err, post){
      if(err){
        res.send(err);
      }
      res.json(post)
    })
  })
  .put(function(req, res){
    Flat.findById(req.params.id, function(err, flat){
      if(err){
        res.send(err);
      }
      return('TODO POST FLAT WITH ID');
    })
  })

這部分代碼有效,在郵遞員中,我有一個正確的數組。 這是我的路線文件的一部分:

angular.module('appRoutes', ['ngRoute', 'ngResource'])
  .config(function($routeProvider, $locationProvider){
    $routeProvider
      .when('/flats/:id', {
        controller: 'FlatController',
        templateUrl: 'app/views/pages/edit.html'
      })          
    });

下一部分-這是帶有所有單位的桌子。 單擊時,我想打開一個帶有單個平面的頁面

<table class="table table-bordered table-hover">
  <thead>
    <tr>
      <th ng-repeat="field in flat.fields">
        {{ field }}
      </th>
    </tr>
  </thead>
  <tbody>
    <tr ng-click="flat.showflat(flatone._id)" ng-repeat="flatone in flat.flats">
      <td ng-repeat="field in flat.fields">
        {{ flatone[field] }}
      </td>
    </tr>
  </tbody>
</table>

這是我的控制器的一部分:

angular.module('flatCtrl', ['flatoneService'])
  .controller('FlatController', ['FlatOne', '$filter', '$location', '$timeout', '$interval', '$routeParams', function(FlatOne, $filter, $location, $timeout, $interval, $routeParams){
    vm = this;
    vm.flats = FlatOne.query();
    vm.showflat = function(_id){
      var flatId = _id;
      vm.flatsToEdit = FlatOne.get({id: $routeParams._id});
      $location.url('/flats/edit');
    }
  }])

在這個控制器中,我正確地具有_id,但是$ routeParams是未定義的,我不明白為什么。

這是我的服務:

angular.module('flatoneService', [])
  .factory('FlatOne', function ($resource){
    return $resource('/api/flats/:id', {id:'@_id'}, {
      'update': {method: 'PUT'}
    });
  });

結果,當我單擊該行時,我有一個正確的URL,例如http:// localhost:3000 / flats / 55c59f11cbe8d2b4105c149c和錯誤:SyntaxError:Unexpected token <。 我希望獲得app / views / pages / edit.html,但獲得我主頁的html代碼。

SyntaxError:意外的令牌<在eval(本機)在https://code.jquery.com/jquery-1.11.3.min.js:2:2622在Function.m.extend.globalEval( https://code.jquery .com / jquery-1.11.3.min.js:2:2633 )位於m.ajaxSetup.converters.text腳本( https://code.jquery.com/jquery-1.11.3.min.js:5:26520 )在Pb( https://code.jquery.com/jquery-1.11.3.min.js:5:18363 )在x( https://code.jquery.com/jquery-1.11.3.min.js :5:21793 )位於m.ajaxTransport.a.send.b( https://code.jquery.com/jquery-1.11.3.min.js:5:26030 )位於Object.m.ajaxTransport.a.send ( https://code.jquery.com/jquery-1.11.3.min.js:5:26134 )在Function.m.extend.ajax( https://code.jquery.com/jquery-1.11.3。 min.js:5:21570 ),位於Function.m._evalUrl( https://code.jquery.com/jquery-1.11.3.min.js:5:22649

您應該更正FlatOne.get調用,您的$routeParam應該是$routeParams.id /可能是因為您已經傳遞了該參數以用作_id ,然后將get id更改為_id因為您有{id:'@_id'}您的$resource

vm.flatsToEdit = FlatOne.get({_id: _id});

您已經傳遞了id以顯示函數:

ng-click="flat.showflat(flatone._id)"

所以我認為沒有必要使用$ routeParams:

vm.showflat = function(_id){
  var flatId = _id;
  //vm.flatsToEdit = FlatOne.get({id: $routeParams._id});
  vm.flatsToEdit = FlatOne.get({id: _id}); // may be this?
  $location.url('/flats/edit');
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM