Simon De Uvarow
Simon De Uvarow

Reputation: 90

ng-options inside ng-repeat - can't access selected item in $scope

I want to do the following:

I have an array of all possible items 'all'. I want to create a subset of that array in 'subset':

js.js:

var app = angular.module('myApp', []);

app.controller('myController', function($scope) {
  $scope.all = [];
  $scope.subset = [{name: 'n1', inUse: false}, {name: 'n2', inUse: false}, {name: 'n3', inUse: false} ];

  // adding new item in all
  $scope.addItem = function() {
    var newc = {name: '?', inUse: false};
    $scope.all.push(newc);
  };

  $scope.updateC = function(index) {
    // index refers to all array
    $scope.all[index].inUse = false;
    $scope.all[index] = $scope.selectedItem;
    $scope.all[index].inUse = true;
  };
});

HTML.html:

<!doctype html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="angular-1.4.8.js"></script>
    <script type="text/javascript" src="js.js"></script>
</head>

    <body ng-controller="myController">
        <button ng-click="addItem()">Add Item: </button>
        <div ng-repeat="c in all">
            {{c.name}}
           <select ng-options="c as c.name for c in subset | filter: {inUse: false}" 
                ng-change="updateC($index)"
                ng-model="selectedItem"></select>
        </div>
    </body>
</html>

But I'm getting 'TypeError: Cannot set property 'inUse' of undefined'. I see the inUse property in a child scope. Is that behaviour expected? How can I access the selected item in my scope?

I can do the following, but I don't believe it's correct:

var child = $scope.$$childHead;
for (var i = 0; i < index ; i++) {
    child = child.$$nextSibling;
}

$scope.all[index] = child.selectedItem;

What is the correct way of doing what I want?

Upvotes: 1

Views: 237

Answers (1)

Simon De Uvarow
Simon De Uvarow

Reputation: 90

Charlie, you gave me the idea of changing the way I'm adding items to the subset:

JS

var app = angular.module('myApp', []);

app.controller('myController', function($scope) {
  $scope.subset = [];
  $scope.all = [{name: 'n1', inUse: false}, {name: 'n2', inUse: false}, {name: 'n3', inUse: false} ];

  // adding new item in all
  $scope.addItem = function() {
    if ($scope.selectedItem != null) {
        $scope.selectedItem.inUse = true;
        $scope.subset.push($scope.selectedItem);
    }
  };
});

It's not the same I wanted to do, but it works.

HTML

<!doctype html>
<html lang="en" ng-app="myApp">
    <head>
        <meta charset="utf-8">
        <script type="text/javascript" src="angular-1.4.8.js"></script>
        <script type="text/javascript" src="js.js"></script>
    </head>

    <body ng-controller="myController">
        <select ng-options="c2 as c2.name for c2 in all | filter: {inUse: false}" 
                ng-model="selectedItem"></select>
        <button ng-click="addItem()">Add Item: </button>
        <div ng-repeat="c in subset">
            {{c.name}}
        </div>
    </body>
</html>

Upvotes: 1

Related Questions