leaksterrr
leaksterrr

Reputation: 4166

Angular populate ng-repeat and ng-model

In my app I have a widget that is used on multiple pages and the selections made on the widgets are to be transferred between pages. So if I select something in the widget on the homepage and click 'continue' which then takes me to a new page, the widget should be pre-populated with the selections made on the homepage.

My widget follows the following format (although not exact for simplistic explanations):

<li ng-repeat="device in devices track by $index">
    <select name="model[{{$index}}]" ng-model="selectedManufacturer" ng-options"...">
    <select name="model[{{$index}}]" ng-model="selectedModel" ng-options"...">
</li>

It's the values of the ng-models inside the repeat statement that contain the key data in the widget that needs to be transferrable.

The number of devices needs to be transferrable too but that will be easy as devices is just an array of numbers so selectedModel.length -1 will equal the value of devices on the secondary pages.

Is it possible to pre-populate the values of ng-models?

Upvotes: 0

Views: 529

Answers (2)

Bose_geek
Bose_geek

Reputation: 498

If you want to pre-populate the value in your ng-model which would mean a default/inital value in your drop-down. This can be achieved by doing the following:-

$scope.selectedManufacturer = listOfManufacturer[0];
$scope.selectedModel = listOfSelectedModel[0];

Upvotes: 0

risto
risto

Reputation: 1304

Yes, you can prepopulate values of ng-models (or any scope variable) using ng-init

Upvotes: 1

Related Questions