0

I have a dropdown where items are populated from $scope.role. Now I need to remove the values from $scope.role once the value is added or selected in dropdown. I did splice(index,1) which actually delete the first element only. Need assistance.

$scope.role = ['Actor', 'Director/ Asst. director', 'Lyricist',
 'Music director/ Asst. director', 'Singer', 'Standup Comedian', 'Model',
 'Cinematographer', 'Photographer', 'Script Writer', 'Choreographer',
 'Editor/ Promo editor', 'Costume designer', 'Art director', 'Stunt artist',
 'Voice-over artist', 'Graphic Designer', 'Screenplay', 'Dialogue', 
'Back ground music'];

Html:

 <div class="row newRow">
  <div class="form-group fields col-sm-2 col-xs-4">
    <label>ROLE *</label>
    <select name="role" class="form-control1 drop2" required ng-model="model.role" placeholder="select">
      <option value='' disabled selected>Select</option>
      <option ng-repeat="item in role track by $index" value="{{item}}">{{item}}</option>
    </select>
  </div>
  <div class="form-group col-sm-2 col-xs-4">
    <button ng-click="AddRole()">Click to Add Role</button>
  </div>
</div>

JS:

$scope.multiRoles = [];
$scope.role == $scope.dummy;
$scope.rolesAdded = false;

$scope.AddRole = function(index) {
  debugger;
  if ($scope.model.role !== undefined) {
    $scope.multiRoles.push($scope.model.role);
    $scope.role.splice(index, 1);
    console.log($scope.role);
  }
};
4
  • what do you mean by I did splice(index,1) which actually delete the first element only? do you have duplicate keys in your array? If yes, you wanna delete all the duplicated keys? Commented Oct 6, 2016 at 7:28
  • @NicoletaWilskon you are passing nothing to AddRole. index is undefined. So it deletes the first element only. Commented Oct 6, 2016 at 7:31
  • What I suppose to do for that Commented Oct 6, 2016 at 7:37
  • Yes i have duplicate in array multirole Commented Oct 6, 2016 at 7:38

2 Answers 2

2

You can do it in two ways 1) As suggested by @nikjohn by sending your $index of dropdown in ng-click = "AddRole($index)" and then splice or else

2) You can find the index of the selected option by using the ng-model binded to the dropdown.

     $scope.AddRole = function(){
          debugger;
          if($scope.model.role !== undefined ){
          $scope.multiRoles.push($scope.model.role);
          var index = $scope.role.indexOf($scope.model.role); //just find the right index which is the selected option in dropdown.
          $scope.role.splice(index,1);
                console.log($scope.role);
            }
        };
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you.Its really helpful.var index = $scope.role.indexOf($scope.model.role); this is were i lacked .
Glad it helped you ! Happy coding mate :) One more to suggest use ng-options instead of ng-repeat that has many capabilities in case of drop down.
@NicoletaWilskon can you accept this as an answer mate ?
0

In your HTML, pass $index to the AddRole. Otherwise, the function does not know what $index is, and also include the button within the ng-repeat:

<select name="role" 
  class="form-control1 drop2" required  
  ng-model="model.role" placeholder="select">
    <option value='' disabled selected>Select</option>
    <option ng-repeat="item in role track by $index" value="{{item}}">   {{item}}
        <button ng-click = "AddRole($index)">Click to Add Role</button>
    </option>
</select>

In your Controller, there's an extra = that I've highlighted in my comment:

$scope.multiRoles = [];
$scope.role == $scope.dummy; // Why a `==` here? This should be a `=`
$scope.rolesAdded = false;        
$scope.AddRole = function(index){
   debugger;
   if($scope.model.role.length){ // Cleaner method to verify that the array is non-empty
   $scope.multiRoles.push($scope.model.role);
   $scope.role.splice(index,1);
     console.log($scope.role);
   }
 };

May I also suggest that you use Angular's select implementation with ng-options because:

Choosing between ngRepeat and ngOptions

In many cases, ngRepeat can be used on elements instead of ngOptions to achieve a similar result. However, ngOptions provides some benefits:

  • more flexibility in how the <select>'s model is assigned via the select as part of the comprehension expression
  • reduced memory consumption by not creating a new scope for each repeated instance
  • increased render speed by creating the options in a documentFragment instead of individually
  • Specifically, select with repeated options slows down significantly starting at 2000 options in Chrome and Internet Explorer / Edge.

5 Comments

Hi ,even this delete the first array element .
You also need to put the button within your ng-repeat. Updated the answer
nickjohn. $index is not proper solution in this case
@AnilKumarRam Why's that?
@nikjohn you solution is also right mate but in many cases you dont want to repeat you button element in ng-repeat as you display it only once so that's this solution is not that feasible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.