2016-01-26 46 views
0

我有一個使用AngularJS的rails應用程序,我有一個表單的問題,問題是我想使用複選框發送值true或false,但它只發送true,如果它被選中如果在此之後選中並取消選中,則爲false,但如果用戶未觸摸複選框,則不會將其作爲參數發送。複選框不發送錯誤的值

<div class="checkbox"> 
    <label> 
     <input type="checkbox" ng-model="car"> Do you have a car? 
    </label> 
    </div> 

如果用戶沒有檢查過它,我該怎麼做才能使它發送錯誤?


編輯:整個形式是這樣的,順便說一句,它是關於建立一個民意調查的形式,對汽車用品只是一個例子...

<h1>Create Poll</h1> 

<form ng-submit="addPoll()" style="margin-top:30px;"> 

    <div class="form-group"> 
    <label>Title</label> 
    <input type="text" class="form-control" ng-model="title"></input> 
    </div> 

    <div class="form-group"> 
    <label>Description</label> 
    <textarea type="text" class="form-control" ng-model="description"></textarea> 
    </div> 

    <br> 
    <div class="checkbox"> 
    <label> 
     <input type="checkbox" ng-model="allow_anonymous_answer" ng-false-value="false"> Allow anonymous answers 
    </label> 
    </div> 
    <br> 

    <div class="form-group"> 
    <label>Welcome message</label> 
    <textarea type="text" class="form-control" ng-model="initial_message"></textarea> 
    </div> 

    <div class="form-group"> 
    <label>Outgoing Message</label> 
    <textarea type="text" class="form-control" ng-model="final_message"></textarea> 
    </div> 

    <button type="submit" class="btn btn-primary" style="float: right;">Continue</button> 

</form> 

當你打我繼續做與Restangular HTTP POST請求創建一個民意調查,但問題是,當我不碰的複選框,這是我的日誌的Rails中看到...

Started POST "/polls.json" for 127.0.0.1 at 2016-01-26 14:05:57 -0300 
Processing by PollsController#create as JSON 
    Parameters: {"title"=>"asddddddddddddddda", "description"=>"aaaaaaaaaaaaaaaaaaaaa", "initial_message"=>"asdasdddddddddd", "final_message"=>"aaaaaaaaaaaaaaaaaaad", "poll"=>{"title"=>"asddddddddddddddda", "description"=>"aaaaaaaaaaaaaaaaaaaaa", "initial_message"=>"asdasdddddddddd", "final_message"=>"aaaaaaaaaaaaaaaaaaad"}} 

不e參數allow_anonymous_answer甚至沒有出現,如果我檢查複選框,然後我可以看到該參數設置爲true,如果我檢查它然後取消選中它,那麼它被設置爲false,但問題是當用戶甚至不碰這個,當這種情況發生,則參數甚至沒有顯示...

就在你想看到的情況下,這是AngularJS的控制器......

angular.module('myapp').controller('CreatePollCtrl', ['$scope', 'Restangular', 
function($scope, Restangular) { 
    Restangular.setFullResponse(true); 

    $scope.addPoll = function() { 
    var poll = {title: $scope.title, description: $scope.description, allow_anonymous_answer: $scope.allow_anonymous_answer, initial_message: $scope.initial_message, final_message: $scope.final_message}; 
    Restangular.all('polls').post(poll).then(function(response) { 
    }); 
    }; 
}]); 
+0

你用阿賈克斯?你可以添加整個表單嗎? – Vanojx1

+0

你能提供一個小提琴嗎? – eslotwinski

+0

我沒有使用ajax,只是角度,我會用整個表單更新帖子。 – OiciTrap

回答

1

我想你應該把一個變量在你的控制器來實現你的HTML組件和你的JS代碼之間的綁定。

我目前正在開發一個角度應用程序,我做的是初始化在我的控制器的第一行中的所有NG-模型變量,那麼你爲什麼不試用一下這個:

在你的第一個控制器線路:

$scope.allow_anonymous_answer = false; 
+0

這是做OP的答案的正確方法。但是,應該遠離$ scope,而使用'controllerAs'。 – ragerory

0

ng-click添加到checkbox並在那裏更新模型。工作正常。

<div class="checkbox"> 
    <label> 
     <input type="checkbox" ng-model="car" ng-click="updateCar(this)">Do you have a car?</input> 
    </label> 
</div> 

在你的控制器:

var updateCar = function(checkbox) { 
    if (checkbox.checked) { 
     car = false; 
    } 
    else { 
     car = true; 
    } 
} 
0

我解決它......

在控制器

if ($scope.allow_anonymous_answer == null) 
    $scope.allow_anonymous_answer = false 
+0

你應該採取@xiidarkevil方法並創建一個'initialize()'函數,將變量設置爲'false'以開始。 – ragerory

+0

哦,我沒有意識到這種反應,但我沒有看到太大的差異,兩者都是正常工作的解決方案。 – OiciTrap

+0

是的,但閱讀不使用'$範圍'...沒有考慮在角度世界的最佳做法。 http://codetunnel.io/angularjs-controller-as-or-scope/ – ragerory