Copied to clipboard

Flag this post as spam?

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


  • Tim C 161 posts 528 karma points
    May 17, 2016 @ 16:51
    Tim C
    0

    Angularjs ng-repeat problem

    For various reasons I have been creatin a custom property editor based on a dropdown. This includes extra data-attributes on the options, so I believe the only way to populate these is using ng-repeat

    <div ng-controller="my.DropdownController" >
     <select  data-ID="{{selID}}" onchange="selectParentChange('{{selID}}')" ng-model="model.value" >   
        <option  repeat-end="onEnd()" ng-repeat="item in opts" value="{{item.value}}" ng-selected="{{item.selected}}" data-parentvalue="{{item.parentvalue}}">          {{item.text}}           
        </option>                               
    </select>
    </div>
    

    I am creating opts in the controller eg

    ..loop through data ...

    optsAR.push ({value:optid,text:opttext,selected:(optid==$scope.model.value?"true":"false"),parentvalue:parentid});
    

    finally

    $scope.opts = optsAR;
    

    The problem is that when this is output to the browser, angularjs outputs a new option automatically when creating a new record because model.value does not match the value of any of the options (naturally - it's a new record)

    <select data-id="myRegion" onchange="selectParentChange('myRegion')" ng-model="model.value" class="ng-pristine ng-valid">
    <option value="? string: ?">
    </option>   
    <!-- ngRepeat: item in opts --><option repeat-end="onEnd()" ng-repeat="item in opts" value="1" ng-selected="false" data-parentvalue="-1" class="ng-scope ng-binding"> All Regions
    </option><option repeat-end="onEnd()" ng-repeat="item in opts" value="2" ng-selected="false" data-parentvalue="-1" class="ng-scope ng-binding">
    Region 1
    </option><option repeat-end="onEnd()" ng-repeat="item in opts" value="5" ng-selected="false" data-parentvalue="-1" class="ng-scope ng-binding">
     Region 2
    </option><option repeat-end="onEnd()" ng-repeat="item in opts" value="4" ng-selected="false" data-parentvalue="-1" class="ng-scope ng-binding"> 
    Region 3
    </option><option repeat-end="onEnd()" ng-repeat="item in opts" value="3" ng-selected="false" data-parentvalue="-1" class="ng-scope ng-binding"> 
    Region 4
    </option>       
    </select>
    

    Ie the problem is there is now

     <option value="? string: ?">
    

    which means if property is set to 'Field is mandatory' it won't throw an error (I assume Umbraco only throws an error if the value is null?) on save.

    Standard Umbraco select dropdowns don't do this, so there must be some code I am missing, or is there a work around?

    I was thinking about doing my own validation, but can't find a way to access the Umbraco 'Field is mandatory' flag in a controller.

    (oddly, as a postscript, even when I load an existing document which does have a value set and it does match an option, I still get the rogue option sometimes. I am wondering if it's a timing issue)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies