2017-07-24 135 views
-1

我有一種表單,我想傳遞一些數據並將它們發佈到URL中。 我在這裏做錯了什麼?由於無法將數據從模板傳遞到控制器angularjs

template.html

<div ng-controller="statusController"> 
<form role="form" name="form" method="post" ng-submit="rate(ratingData)"> 
    <div class="form-group float-label-control"> 
     <label for="title">Title</label> 
     <input id="title" name="title" type="text" class="form-control" placeholder="Title" ng-model="ratingData.title"> 
    </div> 
    <div class="form-group float-label-control"> 
     <label for="rating">Rating</label> 
     <input id="rating" name="rating" type="text" class="form-control" placeholder="Rating" ng-model="ratingData.rating"> 
    </div> 
    <div class="form-group float-label-control"> 
     <label for="review">Review</label> 
     <input id="review" name="review" type="text" class="form-control" placeholder="Review" required ng-model="ratingData.review"> 
    </div> 
    <div class="form-group float-label-control text-center "> 
     <button type="submit" class="btn btn-primary" formmethod="post">Submit</button> 
    </div> 
</form> 

controller.js

angular.module('deliveryStatus', ['ngRoute']) 
.component('deliveryStatus', { 
    templateUrl: 'components/delivery-status/delivery-status.template.html' 
}) 
.controller('statusController', function ($http, $scope, $routeParams, Auth, $window, $timeout, $location) { 

    $scope.rate = function (ratingData) { 
     $location.url('/rating'); 
     $http.put('/api/rating-detail', $scope.ratingData).then(function (res) { 
      console.log('worked'); 
     }).catch(function (err) { 
      console.log('error rating'); 
     }); 
    }; 
}); 
+0

這看起來很愚蠢,但是控制器也被命名爲'statusController'? –

+0

@DanielD是控制器被命名爲'statusController',但我沒有粘貼全部 – XhensB

+0

你可以添加整個控制器代碼嗎? –

回答

0

您的意思是傳遞變量ratingData,然後嘗試從訪問權限的範圍呢?

$http.put('/api/rating-detail', $scope.ratingData).then(function (res) { 

嘗試

$http.put('/api/rating-detail', ratingData).then(function (res) { 
+2

經過反思,這應該不重要。 –

0

我沒有看到任何地方ratingData初始化。看下面的代碼片段,改變它,它應該工作。

angular.module('deliveryStatus', ['ngRoute']) 
    .component('deliveryStatus', { 
     templateUrl: 'components/delivery-status/delivery-status.template.html' 
    }) 
    .controller('statusController', function ($http, $scope, $routeParams, Auth, $window, $timeout, $location) { 
     $scope.ratingData = {}; 
     $scope.rate = function (ratingData) { 
      console.log(ratingData) 

      $http.put('/api/rating-detail', $scope.ratingData).then(function (res) { 
       console.log('worked'); 
       $location.url('/rating'); 
      }).catch(function (err) { 
       console.log('error rating'); 
      }); 
     }; 
    }); 
相關問題