2014-05-08 31 views
0

形式結合:AngularJS數據與傳遞的參數

<form novalidate name="form" ng-submit="addArticle(newPost)"> 
    <textarea ng-model="newPost.content" required></textarea> 
    <button type="submit" ng-disabled="form.$invalid"> 
</form> 

控制器:

angular.module('app').controller('StreamDetailCtrl', function($scope) { 
    $scope.newPost = {}; 

    $scope.addArticle = function(newPost) { 
     // [Post stuff to server] 

     // Clear form 
     newPost = {}; 
    }; 
}); 

這裏的問題是,形式不被清除,沒有數據綁定回事。很明顯,如果我會在方法內部執行$scope.newPost = {};,它的工作正常,但我不能使用它(真正的代碼在不同的服務中有這個addArticle方法,所以我需要將newPost傳遞給它)。

如何綁定這個newPost參數,以便更改它可以更改我的表單?

回答

1

您可以嘗試兩種:

  • newPost.content = null,如果你只是想清除這個屬性
  • angular.copy({}, newPost)將清除一切newPost(如果要清理所有屬性)