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

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?

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

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.

Related

Why do the Angular docs recommend against ng-init in most cases?

From the ng-init 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 initialize values on a scope.
That doc goes on to give the following example:
<div ng-repeat="innerList in list" ng-init="outerIndex = $index">
// [snip]
</div>
What makes it appropriate to use ng-init with ng-repeat but not, for example, with ng-model like this:
<input ng-model="thing.prop"
ng-init="thing.prop = thing.prop || 'defaultValue'">
The doc says one should "use controllers rather than ngInit". What benefit does a controller offer in this case? Is this an Angular stylistic preference, or are there cases in which code like the above will not work?
That's because the documentation should be as concise as posible, and if in each example they create a Controller, it won't be so precise and clear

Angular JS encoded double quotes not working

I am using ngInit to pass variables from PHP to my Angular JS Controller.
In some situations the passed string might contain encoded '"' (Double quotes ")
<div data-ng-controller="UserController" data-ng-init='init({"test":""My Test Input""})'>
</div>
But when this happens I am getting the following error in Angular JS :
http://errors.angularjs.org/1.3.13/$parse/syntax?p0=My&p1=is%20unexpected%2C%20expecting%20%5B%7D%5D&p2=16&p3=init(%7B%22test%22%3A%22%22My%20Test%20Input%22%22%7D)&p4=My%20Test%20Input%22%22%7D)
Please help
Try
<div data-ng-controller="UserController" data-ng-init='init({"test":"\"My Test Input\""})'>
This is an improper use of ng-init and you should run your code in controller instead
From docs:
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 initialize values on a scope.

Variables variables possible in angularjs?

Im working in a basic translation service for angularjs, I have an object in the view with the translations this way:
var translations = {"name":"Name", "address":"Address", "phone":"Telephone"};
So I want to replace if I found the {{phone}} in the view with the value of its translation: "Telephone".
Is there some way to call variables variables while iterate, like this:
<div class="item item-text-wrap" ng-repeat="(k, v) in profile_fields">
<b>{{translations. {{k}} }}</b>
</div>
Thanks in advance!
Sure, just use this:
<b>{{translations[k]}}</b>
Basically, access the translations object like you would in JavaScript, using the k variable.
Keep in mind that you never have to nest those brackets ({{}}) deeper than this.
Also, Angular Translate is a pretty nice translations library. You might want to have a look at it.

How do I get the current child-scope of a ngRepeated element in AngularJS?

This seems like a simple question, but I've been googling around for a while and can't seem to find it.
In my JS I have something called parseTags(book) that takes a JSON comma-separated list of tags (book.tags) and parses it into an array:
$scope.parseTags = function(book){
book.tags = book.tags.split(',');
};
In my HTML I have something like this:
<div ng-repeat="book in books" ng-init="parseTags(book)">{{book.title}}</div>
Is there a way just to get the child scope from within the $scope.parseTags function? Instead of passing in book each time?
Something like:
$scope.parseTags = function($childScope){
$childScope.tags = $childScope.tags.split(',');
}
<div ng-repeat="book in books" ng-init="parseTags()">{{book.title}}</div>
parseTags function is executed in context of the current child scope. So parseTags can also be written as:
$scope.parseTags = function() {
this.book.tags = this.book.tags.split(',');
};
Demo: http://plnkr.co/edit/DUkDVOMjjj0khh5KCYo7?p=preview
you should only use ng-init for special cases when you need to use a property of the ng-repeat, I think you will better doing this kind of functionality in the controller. Unless there is a really specific reason you can't do that. I haven't seen your use of tags in the html, but looks like the kind of functionality a filter would do.
From angularjs docs:
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 initialize values on a scope.
source: angularjs docs

using javascript inline in angularjs

I have an array in my controller containing date-objects called $scope.events. I would like to iterate these events and and print them out in a certain format, which I use momentjs for.
Now the thing is, I cannot get it to actually use momentjs.
I have tried the following:
<table>
<tr ng-repeat="ev in events">
<td>{{ moment(ev).format("HH") }}</td>
</tr>
</table>
but this just prints an empty cell.
So my question is, how do I use javascript, momentjs, inline in my angular-binding ?
thanks
Thomas
Assuming moment is a property of window, you'll need to create a reference in this object's $scope that references moment.
Very simply:
$scope.moment = window.moment;
Here's a plunkr showing internal $scope methods vs. a $scope property referencing a method on window:
http://embed.plnkr.co/PWFK80/preview
That's the simple answer, but you'd likely want to wrap this library into its own directive or service, so that you could use it without coupling higher-level objects to the window object unnecessarily.
Wrap the code in a function in your controller for a quick answer.
controller('Ctrl', function($scope) {
$scope.moment = moment;
});
I edited this to match a comment by #Stewie because it looks better
and in your html:
<td>{{moment(ev, 'HH'}}</td>
Or make a service/directive for moment for something better.

Categories

Resources