For example:
<select ng-model="selectedCategory" ng-options="category as category.title for category in categories"></select>
Obviously, this will not work:
<button ng-click="removeCategory($index)">remove</button>
How could $index be accessed if not in a repeater?
You shouldn't need to keep track of the index, simply remove the selectedCategory from the categories model in the removeCategory function:
Your controller might look like this JSFiddle:
app.controller("myCtrl", ['$scope', function($scope){
$scope.model = {
selectedCategory: {},
categories: [
{title: "Cat1"},
{title: "Cat2"}
]
}
//init
$scope.model.selectedCategory = $scope.model.categories[0];
$scope.removeCategory = function(){
var ind = $scope.model.categories.indexOf( $scope.model.selectedCategory );
$scope.model.categories.splice( ind, 1 );
$scope.model.selectedCategory = $scope.model.categories[0];
}
}])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With