I am having an issue with Angular interpolation.
I have a directive template that looks like this:
<div class="autocomplete {{attrs.class}}" id="{{attrs.id}}">
<input type="text" {{attrs.autofocus | toAutofocusText}} ng-model="searchParam" placeholder="{{attrs.placeholder}}"/>
...
</div>
The problem I have is that this expression is not evaluated:
{{attrs.autofocus | toAutofocusText}}
However, if I change this to something like this:
x="{{attrs.autofocus | toAutofocusText}}"
It does get evaluated.
Could someone explain me why this would be happening? I am guessing it is something fairly fundamental, but I can't find anything on Google.
So the answer is that attributes don't get interpolated on that level. Or to be more specific, at the time when your HTML is compiled, angular processes DomElements. A Dom element with an attribute that starts with {{ isn't a valid attribute, which is why the $compile don't know about it. However, it does now about such expressions within attribute values, or Dom element contents.
Remember that angular is (they say) what html would be if written for applications. The data binding syntax is evaluated as html. Saying:
x="{{attrs.autofocus | toAutofocusText}}"
is different because now you are talking about an attribute's property being evaluated not some unknown characters inside an html element.
Another thing I would recommend is you look into using ng-class for manipulating the class stuff:
http://docs.angularjs.org/api/ng/directive/ngClass
Related
Let's say that I have a form name "signupForm". When I use the ng-disabled directive on a button (to disable the form in case its invalid), I use
ng-disabled="{{formName}}.$invalid"> (formName contains the value signupForm)
When I inspect the button in browser, the above directive evaluates to ng-disabled="signupForm.$invalid">. This is perfect. The problem is that when I try to use the same expression inside of the ng-messages directive, like: ng-messages ="{{formName}}.$error"> the expression is NOT interpolated. So if I use the directive on a div and inspect it in browser, I see it as:
<div ng-messages="{{formName}}.$error"></div> whereas I expect it to be shown as <div ng-messages="signupForm.$error"></div>. But this does not happen.
So what can be done to make the ng-messages directive interpolate the expression & show it correctly? I have tested this issue with AngularJS 1.4.7 & 1.5.0-rc.1 and the issue exists in both of them. Any help is appreciated. Thank you.
ng-messages=this[formName].$error
ng-messages doest not allow interpolation in attribute value.
but we can pass expression to be evaluated.
this in html represent current scope and it has all the information about form with validations too. you can log it and see yourself.
ng-messages="this[formName][fieldName].$error"
ng-messages="this[formName].$error"
will work, worked for me
May be This is a simple question but it is challenging for me.
In angularJS when i write {{}} in html code so i write code like this like
if i talk about dynamic id, we write like code this
<div ng-repeat = 'item in itmes track by $index'>
<div id = "div-{{$index}}">{{item.name}}</div>
</div>
If i use any model without {{}} i write this example
<input id = 'name' ng-model = "item.name"/>
whenever i am coding in angular js, i write code without {{}} then if it is not work then i try code with {{}} and vise versa. and randomly 1 will correct
Question is when i write code with {{}} and without {{}} in html code ?
After the OP explained what exactly was the problem.
So, the question here is very simple: when do we use {{}} and when we don't in the context of ng-model.
When you do a <input type=... ng-model="someModel>, what you're essentially telling Angular is: "Here is an input element; attach $scope's someModel variable to the value of this input element.
Now, you can use this in your JavaScript controller like so: $scope.someModel, but what about HTML? We have a templating language in Angular, and when you give it some variable (say someModel), it'll search its $scope for it, and then put in the value there. If it is unable to, it'll throw a nasty error.
In essence, {{}} GETS the value, without that, you generally set the variable to gold the value of something.
Very simply put, AngularJS thinks that the content within the brace is an expression, which must be resolved. Once it is, Angular puts the value of the resolved expression there. In the most basic of the terms, you just tell it: "Here is some expression; put the evaluated value instead."
In ES6, we call it template strings.
So, you'll use it for expressions which mutate after every iteration. Say, a loop or something. Places where you know what the answer is, or you have static content, you won't use it.
Say you have the following bit of code:
...
...
$scope.figureOne = 10;
in your controller.js and the following in its view file:
<div>My age is {{ figureOne }}</div>
Angular gets the value from the $scope, and puts it there; so, the rendered form will be: My age is 10. However, if you had the following
<div>My age is figureOne</div>
This time, Angular knows that there is nothing to evaluate or resolve, so, it'll just render it as it is: My age is figureOne.
I hope I made it clear! :)
Angular directives have different types of parameters. Some parameters (#) expect string values and some expect javascript expressions (=) (with variables bound to $scope).
There's no obvious way to know which parameter expects what type of value (aside from looking at documentation).
If a variable expects static string value and you have an angular expression
then you'll need to evaluate it by wrapping in {{}}
If there variable expects an expression and you have an expression
simply type that in.
It's the best to avoid using {{}} where possible, your dynamic ID will fail when Angular hasn't interpolated the expression yet, use ng-attr-id="div-{{$index}} for that. https://docs.angularjs.org/guide/directive#-ngattr-attribute-bindings
Another example, if you have a slow connection and Angular isn't loaded yet users will see the {{}}, you can avoid this by using ng-bind="".
See this thread for more info: AngularJS : Why ng-bind is better than {{}} in angular?
It is very simple.
{{something}} - used for one way binding(Static).
<input type="text" value="{{something}}">
Now if you change its value in HTML ,you can not get it by $scope.something in js.
but If you use ng-model="something",you can get its value in JS.
This happens because ng-model is two way binding.
<input type="text" ng-model="something">
Mostly We use ng-model for forms and {{}} to display static information like User details or else.
I was studiously reading the AngularJS API reference for the ngSwitch directive when I came to that part :
place an expression on the on="..." attribute (or the ng-switch="..."
attribute)
So of course I was wondering why there are two ways to place the expression since both of them seems to work the same way.
<div ng-switch="expression">
<div ng-switch on="expression">
I assume there is a reason but I do not find anything for now.
Plunker to test : http://plnkr.co/edit/VAq1t4744bnNC6RgZtEn?p=preview
There is no difference.
You can check the sources of ng-switch directive
https://github.com/angular/angular.js/blob/master/src/ng/directive/ngSwitch.js
var watchExpr = attr.ngSwitch || attr.on
I am creating a directive in which template I need to use the a scope's variable value as the name of the directive (or alternatively controller) to load.
Say I have a directive widget that has a template called widget.html which looks like:
<div class="widget widget.type" {{widget.type}} ng-controller="widget.type">
<div class="navBar">
<div ng-include="widget.type + '-t.html'"></div>
<i class="fa fa-close"></i>
<hr>
</div>
<div ng-include="widget.type + '-f.html'"></div>
</div>
Now widget.type is not getting evaluated in the first line. It works fine for ng-include. Say widget.type's value is weather. The first line should then be interpolated first to look like (doesn't matter if class attribute, widget.type-attr or ng-controller is interpolated)
<div class="widget" weather>
and then compiled to include the weather directive.
How can I get widget.type interpolated in the template?
Not an option is to use ng-include to load the directive. I need to use one common template for the widget and want to add/override/extend the base directive with additonal functionality/Variables.
If this is not the way to achieve that, is there a way to extend a directive the OOP-way?
See the plunkr
You can only place interpolation expressions in text nodes and attribute values. AngularJS evaluates your template by first turning it into DOM and then invoking directive compilation, etc. If you try to place {{...}} instead of attribute name, you'll just end up with messed-up DOM.
If you really need to replace a whole directive based on $scope variable value, you'll need to create a directive for application of other directives and do some heavy lifting with $compile (you'll have to completely re-compile the template each time the value changes). I'd recommend trying to find other designs solving your situation before attempting this.
For adjusting your template based on element attributes, see this answer.
I can easily bind data to a div or pre tag with the code:
<div id="json_route{{route.id}}" ng-bind="items.route{{route.id}} | json"></div>
However, I want to try and bind this data to a hidden form input, I tried:
<input type="hidden" name="json_route{{ route.id }}"
ng-model="items.route{{route.id}} | json" />
Which returns me an error of:
Error: Non-assignable model expression: items.route2 | json (<input type="hidden" name="json_route2" ng-model="items.route2 | json">)
So obviously I cannot use | json when using ng-model. The angular docs are still a bit sparse and I can't seem to find how to assign this correctly, even if I can? Thanks :)
I need to get this json data loaded into my pyramid application, and assigning it into a hidden form field seemed the best way todo it, or should I be doing this in a different way?
"To be able to render the model into the view, the model has to be able to be referenced from the scope." (src: Angular Guide).
Angular needs to be able to reference the value in your ngModel expression to a $scope variable in your controller.
With ng-bind it worked, because ng-bind is not the same as ng-model. ngBind simply takes your expression and evaluates it inside the current scope and than replaces the text of the host element with the result. As Guide tells us, the value of ng-model must be an assignable angular expression to data-bind to.
To have your hidden input contain the json string representation of your 'items.route2' model you could setup a $watch expression in your controller which would "prepare" the json string of your model whenever it changes. See plnkr example.
Try using ng-init:
<input type="hidden" name="..." ng-model="items.route{{route.id}}"
ng-init="items.route{{route.id}} = data_from_server_here">
See also https://stackoverflow.com/a/12657601/215945.