Angular.js – tips

Using an object as a model

<input type=”text” id=”headline” ng-model=”newOfferData.Headline” /> doesn’t work.

console.log($scope.newOfferData); shows nothing

console.log($scope.newOfferData.Headline); works

 

 

<input type=”text” id=”headline” ng-model=”newOfferData[‘Headline’]” /> this works

console.log($scope.newOfferData); shows an object with values set

 

If you want to have different values and labels in select, follow this syntax:

<select name=”campaigns” ng-options=”c.Id as c.Name for c in campaignList” ng-model=”offerData.Campaign”></select>

where campaignList is an array of campaign objects in $scope; campaign is {Id, Name}.

 

When using <select> with ng-options, a ng-model must be present, otherwise no options will be shown

<select name=”existingOffers” ng-options=”o.Headline for o in offersList” ng-model=”editedOffer”></select>

Leave a Reply

Your email address will not be published. Required fields are marked *