Lisa Solomon
Lisa Solomon

Reputation: 136

angularjs - ng-model not setting default values for me

ng-model was set up here so i could just pass filter into my function and get all user selections. I thought I could also use it to prefill/default the data too. It's not working. This is my controller:

.controller('ListCtrl', [
    '$scope', '$filter', '$location', 'context', 'breeze', 'Service',
    function ($scope, $filter, $location, context, breeze, Service) {
        $scope.pageLoaded = true;
        $scope.lists = [];
        $scope.DDL1 = '';
        $scope.filter = {
            fromDate: '2015-05-01',
            toDate: '',
            sales: false            
        };

I have html like this:

            <md-content ng-controller="ListCtrl" layout="column" flex class="md-padding">
                <md-tabs class="md-primary clearfix" md-selected="0" flex>              
                    <md-tab label="Lists">
                        <table><tr>
                                <td width="10%">From Date: </td>
                                <td width="40%"><input type="date" ng-model="filter.fromDate" value="{{filter.fromDate}}"/></td>
<td width="10%">SParts: </td>
                                <td width="40%">                                    
                                    <input type="radio" name="sales" ng-model="filter.sales" value="true"> yes
                                    <input type="radio" name="sales" ng-model="filter.sales" value="false" checked> no
                                </td>...

Upvotes: 1

Views: 372

Answers (2)

Neha Saggam
Neha Saggam

Reputation: 175

You can use ng-checked of angularjs for prefilling the radio button based on some other value.

for example:

<td width="40%">                                    
  <input type="radio" name="sales" ng-model=vm.filter.sales > yes
  <input type="radio" name="sales" ng-model=vm.filter.sales ng-checked="true"> no
</td>

In the above example I have directly set the ng-checked value to true but you can also set it to true or false using the controller variable also. For more information you can check the documentation @ https://docs.angularjs.org/api/ng/directive/ngChecked

Upvotes: 1

Jossef Harush Kadouri
Jossef Harush Kadouri

Reputation: 34257

Use string

Since your'e using input type="radio", set the type of filter.sales to string, that should work

$scope.filter = {
    fromDate: '2015-05-01',
    toDate: '',
    sales: 'false'          
};

From input type="radio" ng-model docs,

The value to which the ngModel expression should be set when selected. Note that value only supports string values, i.e. the scope model needs to be a string, too. Use ngValue if you need complex models (number, object, ...).

Upvotes: 3

Related Questions