I have the following title, and I want to show an element if person == some value, e.g family. How can I build that expression with ngShow?
<h2 class="mat-title" style="text-align:center;">Register as a {{person}}</h2>
You can use ng-if
<h2 ng-if="person==='family'" class="mat-title" style="text-align:center;">Register as a {{person}}</h2>
Related
How to select p tag and b tag both and if p and b have same element then only one should be selected.
<html>
<div>
<p><b>This is first line</b></p>
<b>This is second line</b>
<span style="color:blue">This is third line</span>
</div>
</html>
How do I select all the three lines only once?
If I use html.select("p,b");, <p><b>This is first line</b></p> gets selected twice.
You need the pseudo-selector :not to exclude the child b tag.
for (Element e : doc.select("p :not(b),b"))
System.out.println(e.ownText());
Output
This is first line
This is second line
To include the third line, add span to your selector.
for (Element e : doc.select("p :not(b),b,span"))
System.out.println(e.ownText());
Output
This is first line
This is second line
This is third line
How do I select all the three lines only once?
Use the parent with a selector for child nodes div>* (see CSS reference):
Update: use selector for elements, that are not <div> but a have a <div> parent .select("div>:not(div)")
String htmlString = "<html><div><p><b>This is first line</b></p><b>This is second line</b><span style=\"color:blue\">This is third line</span></div></html>";
Document doc = Jsoup.parse(htmlString);
Elements elements = doc.select("div>:not(div)");
for (Element element : elements) {
System.out.println(element.toString());
}
This prints out:
<p><b>This is first line</b></p>
<b>This is second line</b>
<span style="color:blue">This is third line</span>
If you will only use the text inside the nodes simply use element.text()
Assuming that I have an expression-like string in my scope
$scope.expressionString = 'model.key == "anything"'
I want to use this string as an expression in view, can I do that?
In view, I will have something like
<div ng-if="expressionString"></div> but of course, expressionString should be something else instead.
I appreciate any help. Cheers!
You can use $eval to evaluate your expression , there are two ways to do it in your case
Solution 1
<div ng-if="$eval(expressionString)"></div>
Solution 2
In the controller store the evaluated value of the expression like below
$scope.expressionString = $scope.$eval('model.key == "anything"')
and then in the view simply use it without using $eval in the view
<div ng-if="expressionString"></div>
I found the answer, made a parse filter to parse the string and assign it a scope
angular.module('zehitomo')
.filter('parse', function ($parse) {
return function (expression, scope) {
return $parse(expression)(scope);
};
});
And in view
ng-if="expressionString | parse:this"
You cannot use global variables (or functions) in Angular expressions. Angular expressions are just attributes, so are strings and not Javascript code.
Please see this stackoverflow answer once
Although, you can achieve it using a function instead of a variable:
$scope.expressionString = function(toCompare) {
return $scope.model.key == toCompare;
}
and in your view:
<div ng-if="expressionString('anything')"></div>
I have a $scope.gemiddeldeCijfer. Each object in this scope has two values, a cijfer and aantalDecimalen. I would like to use the value of aantalDecimalen as the number in a number filter.
This is the html, as you can see the number filter now has a set value of 1. But I would like to replace this value with the value of cijfer.aantalDecimalen.
<li ng-repeat="cijfer in gemiddeldeCijfer">
<b>{{cijfer.cijfer | number:1}}</b> - <b>{{cijfer.aantalDecimalen}}</b>
</li>
http://jsfiddle.net/Kx4Tq/87/
So you want
{{cijfer.cijfer | number:cijfer.aantalDecimalen}}
How can I do an ng-hide inline expression like this:
ng-hide="userType!=user"
?
what is your userType ?? a string ? and you want to hide if != 'user'
ng-hide="userType!='user'"
please check this answer about ng-hide/ng-show
The ngHide directive would not work on an inline expression. The inline expression is evaluated and then the result is injected in the HTML element containing the expression.
If the inline expression is just plain text, you could try the following:
{{ userType != user ? "" : value }}
or if you do not want an empty string you could also use "null"
{{ userType != user ? null : value }}
I'd like to make something like this.
<h3 ng-show="{{mode == 'create'}}">Create Vacancy</h3>
<h3 ng-show="{{mode == 'edit'}}">Edit this Vacancy</h3>
Where $scope.mode is either "create" or "edit".
How do I do this? Nothing I'm trying is working.
ng-show evals expression itself, so don't use interpolated text. Update your code to:
<h3 ng-show="mode == 'create'">Create Vacancy</h3>
<h3 ng-show="mode == 'edit'">Edit this Vacancy</h3>