简体   繁体   English

AngularJS ng-model和ng-checked单选按钮滑块

[英]AngularJS ng-model and ng-checked radio button slider

I'm trying to update an ng-model when a radio button is checked. 选中单选按钮时,我正在尝试更新ng-model。 Although my radio buttons are actually images and my radio buttons are being updated by a function using arrows to cycle through them. 尽管我的单选按钮实际上是图像,并且我的单选按钮正在通过使用箭头的功能在它们之间循环更新。

For some reason my $scope.transaction only updates when I actually click the image rather than changing the image using the arrows. 由于某些原因,我的$ scope.transaction仅在我实际单击图像时更新,而不是使用箭头更改图像。 Any idea why? 知道为什么吗?

我的UI

HTML HTML

<div ng-repeat="contact in contacts" 
     ng-show="showContactID == {{contact.contact_id}}">
    <div class="radio text-center">
        <label>
            <input type="radio" 
                   value="{{contact.contact_id}}" 
                   ng-checked="showContactID == {{contact.contact_id}}"
                   ng-model="transaction.contact_id">

            <img src="public/img/profiles/{{contact.contact_id}}.jpg" 
                 class="img-circle" 
                 height="150">
            <h4>{{contact.contact_name}}</h4>
        </label>
    </div>
</div>

<div class="row text-center">
    <div class="col-xs-6">
        <a class="btn" 
           ng-click="changeContact('prev')">
         <i class="fa fa-chevron-left fa-2x"></i></a>
    </div>
    <div class="col-xs-6">
        <a class="btn" 
           ng-click="changeContact('next')">
         <i class="fa fa-chevron-right fa-2x"></i></a>
    </div>
</div>

CSS CSS

.transaction .form-group.contacts input {
    display: none;
}

.transaction .form-group.contacts .radio {
    padding-left: 0;
}

JavaScript JavaScript的

$scope.changeContact = function(which){

    var currentContactID = $scope.showContactID;
    var newContactID = 0;

    if(which){

        angular.forEach($scope.contacts, function(contact, key){

            if(contact.contact_id == currentContactID){

                if($scope.contacts[which == 'next' ? key + 1 : key - 1] == undefined){

                    newContactID = $scope.contacts[which == 'next' ? 0 : $scope.contacts.length - 1].contact_id;

                }
                else{

                    newContactID = $scope.contacts[which == 'next' ? key + 1 : key - 1].contact_id;

                }

            }

        });

        $scope.showContactID = newContactID;

    }

};

每当您调用changeContact函数时,还要在同一函数中使用newContactID更新transaction.contact_id值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM