Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jonathan Roberts 409 posts 1063 karma points
    Jan 15, 2018 @ 12:19
    Jonathan Roberts
    0

    Saving a selected Drop down value in AngularJS (backoffice)

    Hi,

    I have built my own backoffice control in the App_plugins.

    <select ng-model="control.value.size">
            <option selected="selected" value="">Choose a banner size</option>
            <option ng-repeat="size in bannersizes" value="{{size.value}}">{{size.name}}</option>
        </select>
    

    I have populated the values using the following code:

     if ($scope.control.bannersizes && $scope.control.bannersizes.length > 0) {
            $scope.bannersizes = $scope.control.bannersizes;
        } else {
            try {
                $scope.bannersizes = [
                    { name: 'Small', id: 'Small', selected: false },
                    { name: 'Large', id: 'Large', selected: false }
                ];
            }
            catch (err) {
                alert(err.name + ': "' + err.message);
            }
        }
    

    But when I save the control the values are not being stored and the selected values are all set to false:

     $scope.$on("formSubmitting", function (ev, args) {
             //banner themes
             $scope.control.bannersizes = [];
             angular.forEach($scope.bannersizes, function (value, key) {
                 $scope.control.bannersizes.push(
                     { name: value.name, id: value.id, selected: value.selected }
                 );
             });
         });
    

    Am I missing something or am i doing it all wrong?

    Thanks

    Jon

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jan 15, 2018 @ 12:48
    Dan Diplo
    0

    Maybe try using ng-options to generate your select list rather than ng-repeat ?

Please Sign in or register to post replies

Write your reply to:

Draft