Tuesday, September 26, 2017

How can i force angular.js NOT to remove a 0-value from the model?

I have a json object containing some properties, whose values can become 0. They are bound to a set of radio buttons (the first ones value is 0). The problem is now, that if i choose the first radio button, the property will be removed by angular.js by default. But i want to keep it with a zero as value.

I also need a way to preselect the first radio button if the value of the bound property is 0/the property doesn’t exist. The checked=”checked” attribute doesn’t work for any reason it the bound value is 0/doesn’t exist.

Is there a way to achieve it? Thank you.

The radio buttons:

    <input type="radio" name="type" ng-value="0" ng-model="item.type" ng-init="item.type=0" ng-checked="true"/>
    <input type="radio" name="type" ng-value="1" ng-model="item.type"/>
    <input type="radio" name="type" ng-value="2" ng-model="item.type"/>
    <input type="radio" name="type" ng-value="3" ng-model="item.type"/>

The controller:

    $scope.item = {
            type: 0
    };

Source: AngularJS



from Angular Questions https://angularquestions.com/2017/09/26/how-can-i-force-angular-js-not-to-remove-a-0-value-from-the-model/
via @lzomedia #developer #freelance #web

No comments:

Post a Comment