Object equality comparison for input[radio] with ng-model and ng-value

后端 未结 5 1082
长情又很酷
长情又很酷 2020-12-16 14:59

Let me start by saying that this question is very similar to issues with selection in a

提交评论

  • 2020-12-16 15:25

    As OP requested, here's an example radio button directive that will work with complex objects. It uses underscore.js to find the the selected item from the options. It's a little more complicated than it should be because it also supports loading the options and selected value with AJAX calls.

    0 讨论(0)
  • 2020-12-16 15:40

    I write a most simple directive. Using a kind of "track-by" to map two different objects. See the http://jsfiddle.net/xWWwT/146/.

    HTML

    <div ng-app="app">
    <div ng-app ng-controller="ThingControl">    
        <ul >
            <li ng-repeat="color in colors">
                <input type="radio" name="color" ng-model="$parent.thing" ng-value="color" radio-track-by="name" />{{ color.name }}
            </li>
        </ul>
        Preview: {{ thing }}
    </div>
    </div>
    

    JS

    var app = angular.module('app', []);
    
    app.controller('ThingControl', function($scope){
        $scope.colors = [
            { name: "White", hex: "#ffffff"},
            { name: "Black", hex: "#000000"},
            { name: "Red", hex: "#000000"},
            { name: "Green", hex: "#000000"}
        ];
    
        $scope.thing = { name: "White", hex: "#ffffff"};
    
    });
    
    app.directive('radioTrackBy', function(){
    return {
            restrict: "A",
            scope: {
                ngModel: "=",
                ngValue: "=",
                radioTrackBy: "@"
            },
            link: function (ng) {   
                if (ng.ngValue[ng.radioTrackBy] === ng.ngModel[ng.radioTrackBy]) {                                
                    ng.ngModel = ng.ngValue;
                }
            }
        };
    });
    
    0 讨论(0)
  • 2020-12-16 15:41

    Since I'm not yet able to add comments, so I have to reply here. Dana's answer worked ok for me. Although I'd like to point out in order to use his approach, one would have to implement the 'equals' function on the objects in the collection. See below example:

    .controller('ExampleController', ['$scope', function($scope) {
       var eq = function(obj) {
           return this.id === obj.id;
         };
       col = [{id: 1, name: 'pizza', equals: eq}, {id:2, name:'unicorns', equals: eq}, {id:3, name:'robots', equals: eq}];
    
       $scope.collection = col;
       $scope.my = { favorite : {id:2, name:'unicorns'} };
    
     }]);
    

    See the plunker link.

    0 讨论(0)
  • 2020-12-16 15:44

    Why don't you just use the ID for the select like this?

    <input type="radio" ng-model="selectedItem" ng-value="item.ID"> {{item.Label}}
    

    And then instead of using selectedItem you could write items[selectedItem].

    Oh, and while playing with your problem in jsfiddle I noticed to other things:

    a.) You forgot to add a name attribute to the input.

    b.) Don't ever use something without a dot in ng-model. If you actually try to output selectedItem with {{selectedItem}} outside the ng-repeat block, you will notice that the value does not update when you chose a radio button. This is due to ng-repeat creating a own child scope.

    0 讨论(0)
  • 提交回复
    热议问题