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
參數,以便更改它可以更改我的表單?