角度のあるフォームデータを投稿するきれいな方法は何ですか?Angularを使用してフォームデータを投稿する際のきれいな方法?
私は、このフォームのセットアップ
<div id="contact-form" class="row">
<div class="col-sm-4 col-sm-offset-4 text-center">
<form>
<div class="form-group">
<input type="text" class="form-control input-lg text-center" placeholder="Firstname" name="firstname" ng-model="firstname">
</div>
<div class="form-group">
<input type="text" class="form-control input-lg text-center" placeholder="Lastname" name="lastname" ng-model="lastname">
</div>
<div class="form-group">
<input type="email" class="form-control input-lg text-center" placeholder="Email" name="email"ng-model="email">
</div>
<!-- Submit Contact -->
<button type="submit" class="btn btn-primary btn-lg" ng-click="createContact()">Add</button>
</form>
</div>
</div>
を持っていると私はNode.jsのバックエンドの "API" にこれを掲示しています。
これを正しく行うにはどうすればよいですか?すべてのAPIリクエストを1つのコアファイルに書きますか?ページごとに別々のファイルを作成しますか?
そして、どうすればきれいなリクエストを書くことができますか?
$scope.createContact = function() {
$http.post('/contacts', ...)
.success(function(data) {
})
.error(function(data) {
});
};
私は新しい連絡先を追加するには、「ファーストネーム」と「電子メール」、「姓」を処理したいが、オンライン私はこれを書くための良い、きれいな方法を見つけることができません。
は、ここでそれが助け場合、私のモデルです:
var mongoose = require('mongoose');
var ContactSchema = new mongoose.Schema({
firstname: { type: String, require: true },
lastname: { type: String, require: true },
email: { type: String, require: true }
});
module.exportsは= mongoose.model( 'コンタクト'、ContactSchema)。
最後に使用したコードは次のとおりです。
$scope.createContact = function(contact) {
$scope.contact = { firstname: $scope.firstname, lastname: $scope.lastname, email: $scope.email };
$http.post('/contacts', $scope.contact)
.success(function(data) {
$scope.contact = {firstname:'',lastname: '', email:''}; // clear the form so our user is ready to enter another
$scope.contacts = data;
console.log(data);
})
.error(function(data) {
console.log('Error: ' + data);
});
};
あなたのタイトルが2' 'アンギュラ持っていますが、タグは 'angularjs'であり、あなたは'すべての角度倍です$のscope'と '$ http'を使用しています。あなたのタイトルを更新し、 '2'を削除しました。 – Igor