have an select
based on any array. the elements in the array may change. how do I get the angular controller to refresh the array?
var langMod = angular.module('langMod', []); langMod.controller( .controller( 'colorCntl', function($scope) { $scope.color = 'wt'; $scope.colorArr = [ { id: 'br', name: 'brown' }, { id: 'wt', name: 'white' } ]; });
<form ng-controller='wordCntl' > <select ng-model="color" ng-options="c.id as c.name for c in colorArr"> <option value=''>-- chose color --</option> </select> </form>
> scope = angular.element(document.querySelector('select')).scope(); > scope.colorArr.push( { id:'bk', name:'black' } ); 3 note! the select dropdown still only has brown and white, not black
how do I get the select
to refresh so that all elements in colorArr
are options?
Definition and Usage. The ng-options directive fills a <select> element with <options>. The ng-options directive uses an array to fill the dropdown list. In many cases it would be easier to use the ng-repeat directive, but you have more flexibility when using the ng-options directive.
Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.
AngularJS ng-selected Directive The ng-selected directive sets the selected attribute of an <option> element in a <select> list. The option will be selected if the expression inside the ng-selected attribute returns true. The ng-selected directive is necessary to be able to shift the value between true and false .
Definition and Usage The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.
Angular uses watchers, and will only update the UI if a digest loop has been kicked off.
Normally you would be adding to the array via some event in the UI, or by calling the $http service, and those take care of kicking off a $digest() for you.
Since you are just adding directly to the array, Angular does not know anything has changed, and therefore does not update the UI.
Wrap your statement inside of a scope.$apply(function(){ //code });
instead.
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