2016-07-31 58 views
0

我正在使用AngularJS。我想通過AngularJS發送HTTP帖子中的多個數據。我使用laravel php作爲後端,並使用tinymce作爲我的html文本編輯器。問題是,tinymce提供了一個模型,我無法弄清楚如何傳遞處理多個模型的數據。在angularjs中傳遞多個數據

這裏是我的部分:

<div ng-controller="controller"> 

<div class="row"> 
    <div class="container"> 
     <div class="col-lg-8"> 
      <form role="form" method="post" > 
       <div class="form-group" > 
        <input type="text" class="form-control" name="" ng-model="post.title" placeholder="Type the title here"> 
       </div> 

       <div action="" class="form-group" ng-controller="TinyMceController"> 
        <textarea ui-tinymce="tinymceOptions" ng-model="tinymceModel" style="height:300px"> 

        </textarea> 
       </div> 

       <div class="form-group"> 
        <input type="text" class="form-control" id="" ng-model="post.tags" placeholder="Atleast one related tag"> 
       </div> 

       <button type="submit" class="btn btn-primary" ng-click="postsave()">Submit</button> 
       <button class="btn btn-default">Discard</button> 
      </form> 
     </div> 
     <div class="col-lg-4"> 
      RULES: 
     </div> 
    </div> 
</div> 

我的控制器:

app.controller('controller',['$scope','$rootScope','$http',function($scope,$rootScope,$http){ 

$scope.post={ 
    title:"", 
}; 


$scope.postsave= function(){ 
    $http({ 
     method:"POST", 

     url:'http://localhost/../../...', 
     data:  //I can pass title by using $scope.title but i aslo 
        //want to pass $scope.tinymcemodel 


    }) 
    .success(function(result){ 
     alert(result); 
    }) 
} 

任何幫助將是great.Also建議我,如果有這樣做的更好的方法。

謝謝。

回答

1

就像你的post.tags是結構化的,你應該這樣做。把所有的孩子都歸入父母,並提交父母。讓我們看一下:

$scope.formData = {}; //parent 

那麼你的textarea /標題應作相應的調整:

ng-model="formData.title" //child 
ng-model="formData.tinymcemodel" //child 

然後當你在傳遞你的數據,你可以通過this.formData

$scope.postsave= function(){ 
    $http({ 
     method:"POST", 
     url:'http://localhost/../../...', 
     data: this.formData 
    }) 
    .success(function(result){ 
     alert(result); 
    }) 
}; 

這將對象正確發送到服務器,這將有鑰匙:值對您的titletinymcemodel。請注意,post.tags將不會包含在此提交中,因爲它屬於另一位家長。

+0

That worked.Thanks a lot! – uttejh