Wednesday, October 11, 2017

angularJS ng-true-value bind with ng-model

I have problems with checkboxes. Basically, I have checkboxes that include “Other” and if “Other” is checked a textbox appears for the user to input something, is it possible to bind whatever the user has input with ng-true-value? because I don’t want the value of the “Other” checkbox to be true, I want it to be the value of the textbox.

This is what I have in the HTML:

<div ng-repeat="item in $ctrl.questions" layout="column" layout-gt-xs="row">
                <label></label>
                <!-- loop through options in each question -->
                <div ng-repeat="(k, v) in item.Options">
                    <!-- populate check boxes for choices -->
                    <md-checkbox ng-change="$ctrl.itemChange($parent.$index, v)" ng-model="$ctrl.checkedItems[item.Item][v]" ng-true-value="'true'"
                        ng-false-value="'false'"></md-checkbox>
                </div>
                <!-- Check box for "others" if IncludeOther == true -->
                    <md-checkbox ng-if="item.IncludeOther" ng-model="$ctrl.checkedItems[item.Item]['Other']" ng-false-value="'false'" ng-true-value="''"
                        ng-change="$ctrl.itemChange(item.Item, 'Other')">Other</md-checkbox>
                        <input ng-if="item.IncludeOther" ng-model="$ctrl.otherInput[item.Item]" ng-change="$ctrl.itemChange(item.Item, 'Other')">
                    <!-- <input ng-if="$ctrl.checkedItems[item.Item]['Other'] != $ctrl.otherTrueValue[item.Item]" ng-model="$ctrl.otherInput[item.Item]" ng-change="$ctrl.itemChange(item.Item, 'Other')"> -->
            </div>

And in the js file:

angular.forEach(self.checkedItems, function (value, key) {
            console.log(key);
            angular.forEach(value, function(v, k) {
                if(k == 'Other' && v == 'true') {

                    console.log(v);

                    value.Other = self.otherInput[key];
                }
            });
        });

Sorry that I am a beginner and have been struggling the entire day. Thanks in advance 🙂

Source: AngularJS



from Angular Questions https://angularquestions.com/2017/10/11/angularjs-ng-true-value-bind-with-ng-model/
via @lzomedia #developer #freelance #web #lzomedia.com

No comments:

Post a Comment