Angular select within a ng-repeat directive: how can I access the children scopes?

janesconference

I've got an ng-repeat-ed table row, inside wich are a pair of angular selects:

    <div ng-controller="midiCtrl" id="midi-ctrl">
        [...]

                <tr ng-repeat="plugin in plugins">
                    <td><strong>{{plugin.name}}</strong></td>
                    <td>
                       <select class="span1" ng-model="selectedChannel" ng-options="item.ID as item.Title for item in channels">
                    </td>
                    <td>
                        <select class="span2" ng-model="selectedDevice" ng-options="item.ID as item.Title for item in devices">
                    </td>
                </tr>

        [...]
    </div>

The controller is:

    app.controller('midiCtrl', function ($scope, pluginDisplayedWindows) {

        $scope.plugins = pluginDisplayedWindows.pluginsDisplayed; // An object          

        $scope.channels = [
           {ID: 'all', Title: 'All'},
           {ID: '0', Title: '1'},
           {ID: '1', Title: '2'},
           {ID: '2', Title: '3'}
        ];

        $scope.devices = [
           {ID: '0', Title: 'Device A'},
           {ID: '1', Title: 'Device B'},
           {ID: '2', Title: 'Device C'},
           {ID: '3', Title: 'Device D'},
        ];

    });

Now, I know that when one of the selects is selected, the corresponding object is set on the ng-model scope variable ($scope.selectedChannel or $scope.selectedDevice), but obviously it is set in the corresponding ng-repeat child scope.

How can I access the children scopes, in the controller? I want to save all the selections when the user presses a button, but if I try to do that in the midiCtrl controller, I can't access to the children scopes created by ng-repeat.

zs2020

The simplest trick is to add the selected values to current plugin object, so you can easily get the selected values and those values are bound to the correct plugin object naturally. No other objects will be introduced. Very simple.

<select class="span1" ng-model="plugin.selectedChannel" ng-options="item.ID as item.Title for item in channels">
<select class="span2" ng-model="plugin.selectedDevice" ng-options="item.ID as item.Title for item in devices">

Working Demo 1

If you want store it separately, you can do

<select class="span1" ng-model="selected[$index].selectedChannel" ng-options="item.ID as item.Title for item in channels" />
<select class="span2" ng-model="selected[$index].selectedDevice" ng-options="item.ID as item.Title for item in devices" />

$scope.selected = [];
angular.forEach($scope.plugins, function (a) {
    $scope.selected.push({
        selectedChannel: undefined,
        selectedDevice: undefined
    });
})

Working Demo 2

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can I pass a filter into an angular directive to be used in an ng-repeat?

분류에서Dev

Angular Directive needs access to variable in ng-repeat

분류에서Dev

How do I pass a function to an angular directive inside ng-repeat (arg is undefined)?

분류에서Dev

How to access ng-repeat item object into directive and child scope?

분류에서Dev

angular directive ng-repeat scope and Service

분류에서Dev

How can I display custom directive within an ui-view?

분류에서Dev

Directive not able to access ng-repeated children until after a digest

분류에서Dev

Are scopes within each independent directive isolated and private within that directive?

분류에서Dev

How can I access a column of a line from within a string?

분류에서Dev

Select All checkbox to select nested angular ng-repeat checkboxes?

분류에서Dev

How can I select all the text within a Windows Forms textbox?

분류에서Dev

ng-repeat of a directive and $validators

분류에서Dev

Compiling conditionally inserted html in an ng-repeat which is within a directive's template

분류에서Dev

How to create a custom directive for ng-table (using different data for the table and ng-repeat)

분류에서Dev

ANgularjs: ng-repeat and nested custom directive

분류에서Dev

How can I access a class data member from a method within the same class?

분류에서Dev

How to filter objects in array by category key in Angular ng-repeat

분류에서Dev

angular ng-repeat with condition

분류에서Dev

angular filter for the ng-repeat

분류에서Dev

ng-repeat없이 Angular Material md-select 사용

분류에서Dev

How can I place a checkbox within a button?

분류에서Dev

ng-click within an ng-repeat, not formatting values correctly

분류에서Dev

angular select ng-selected 작동하지 않음 (<option ng-repeat> 사용)

분류에서Dev

AngularJs using ng-repeat in a directive gives type error

분류에서Dev

Getting Attribute value in ng-repeat from a Directive

분류에서Dev

How can I set the background of a linearLayout to be at half opacity but not it's children?

분류에서Dev

Define a function for ng-click in an Angular directive

분류에서Dev

Access a ng-repeat variable in a controller

분류에서Dev

element.on('click') on a directive within ng-if doesn't work

Related 관련 기사

  1. 1

    How can I pass a filter into an angular directive to be used in an ng-repeat?

  2. 2

    Angular Directive needs access to variable in ng-repeat

  3. 3

    How do I pass a function to an angular directive inside ng-repeat (arg is undefined)?

  4. 4

    How to access ng-repeat item object into directive and child scope?

  5. 5

    angular directive ng-repeat scope and Service

  6. 6

    How can I display custom directive within an ui-view?

  7. 7

    Directive not able to access ng-repeated children until after a digest

  8. 8

    Are scopes within each independent directive isolated and private within that directive?

  9. 9

    How can I access a column of a line from within a string?

  10. 10

    Select All checkbox to select nested angular ng-repeat checkboxes?

  11. 11

    How can I select all the text within a Windows Forms textbox?

  12. 12

    ng-repeat of a directive and $validators

  13. 13

    Compiling conditionally inserted html in an ng-repeat which is within a directive's template

  14. 14

    How to create a custom directive for ng-table (using different data for the table and ng-repeat)

  15. 15

    ANgularjs: ng-repeat and nested custom directive

  16. 16

    How can I access a class data member from a method within the same class?

  17. 17

    How to filter objects in array by category key in Angular ng-repeat

  18. 18

    angular ng-repeat with condition

  19. 19

    angular filter for the ng-repeat

  20. 20

    ng-repeat없이 Angular Material md-select 사용

  21. 21

    How can I place a checkbox within a button?

  22. 22

    ng-click within an ng-repeat, not formatting values correctly

  23. 23

    angular select ng-selected 작동하지 않음 (<option ng-repeat> 사용)

  24. 24

    AngularJs using ng-repeat in a directive gives type error

  25. 25

    Getting Attribute value in ng-repeat from a Directive

  26. 26

    How can I set the background of a linearLayout to be at half opacity but not it's children?

  27. 27

    Define a function for ng-click in an Angular directive

  28. 28

    Access a ng-repeat variable in a controller

  29. 29

    element.on('click') on a directive within ng-if doesn't work

뜨겁다태그

보관