CodeToad
CodeToad

Reputation: 4734

angular.js: reference the current model by short variable name

suppose I am binding to a model using a long expression such as

 <ul class="container" ui-sortable  ng-model="cssRules.categories['sd-text-highlight-    color']" ng-class="{selected: cssRules.categories['sd-text-highlight-color'] ==    selectedCategory}" ng-click="selectCategory(cssRules.categories['sd-text-highlight-color'])">

is there a way to not repeat cssRules.categories['sd-text-highlight-color'],

and just bind to the currently referenced model using some keyword or assigned variable name?

Upvotes: 0

Views: 484

Answers (2)

PrimosK
PrimosK

Reputation: 13918

Why don't you expose it trough the controller like:

$scope.cssCategory = cssRules.categories['sd-text-highlight-color']

[UPDATE]

With regards to ng-init alternative (from Angular documentation):

The only appropriate use of ngInit is for aliasing special properties of ngRepeat, as seen in the demo below. Besides this case, you should use controllers rather than ngInit to initialise values on a scope.

More info.

This might be a reason that it doesn't work well with ui-sortable so I guess this approach should be avoided.

Upvotes: 2

Jscti
Jscti

Reputation: 14440

Try it with ng-init :

 <ul 
     class="container"
     ui-sortable
     ng-init="mymodel = cssRules.categories['sd-text-highlight-color']"
     ng-model=""
     ng-class="{selected: mymodel  ==    selectedCategory}"
     ng-click="selectCategory(mymodel )"
>

Hope this helps

Upvotes: 2

Related Questions