Is it possible to have multiple statements in an inline if statement in an AngularJS expression? For example, the following fail:
ng-change="someCondition() ? doA(); doB() : doC()"
ng-change="someCondition() ? doA(), doB() : doC()"
This can be done as follows:
ng-change="someCondition() ? doA() : doC(); someCondition() ? doB() : ''"
But that's ugly, calls someCondition() twice, and screams to be done in a controller. Is there any way to get the first examples to work?
Angular expressions are limited to simple expressions. Anything more complicated may be rejected by parser and error will be thrown. Read about differences between Angular Expressions vs. JavaScript Expressions in documentation. Namely:
No Control Flow Statements: You cannot use the following in an Angular expression: conditionals, loops, or exceptions.
No Comma And Void Operators: You cannot use , or void in an Angular expression.
etc.
The best thing you can do is to create a function in controller that will combine doA and doB. Not only it will make Angular parse happy, but the code itself will become cleaner and simper to read. After all this is template, it's better to keep it simple.
Your first example is not valid javascript.
Your second example contains , which cannot be in any angular expression (not sure if valid javascript or not).
Depending on your use case you can sometimes get it to work using && and ||.
However, code in templates is still code. Try to make it readable. If you can't make it readable, extract it into a function in your controller.
ng-change="onChange()"
Please try the below scenario:::
use the () in multiple statements
ng-change="someCondition ? (doA(), doB()) : doC()"
ng-change="someCondition ? (doA(), doB()) : doC()"
I tried something like this in Angular 8 with ternary operator
<input
maxlength="{{memberProofForm.proofTypeId === 1?16:(memberProofForm.proofTypeId === 2?10:(memberProofForm.proofTypeId === 3?15:77))}}"
name="proofIdNumber"
required
type="text"
[(ngModel)]="memberProofForm.proofIdNumber">
It will work like this
if(memberProofForm.proofTypeId === 1){
return 16;
}
}else{
if(memberProofForm.proofTypeId === 2){
return 10;
}else{
if(memberProofForm.proofTypeId === 3){
return 15
}else{
return 77;
}
}
}
Related
Is it possible to do this in terraform?
for i in range(10):
if var != "" and i > 2:
# Something to do
elif var != "" and i < 2:
# Something to do
else:
# Something else to do
What i want to achieve is to create list but i need if/else statement in for loop. What i have achieved so far is:
for i in range(10):
if var != "":
# Something to do
It's hard to answer this question because it seems like you already decided on a solution to a problem but you haven't actually stated what the underlying problem is. However, I will try to answer directly what you asked, nonetheless.
When thinking about approaching problems in Terraform it's best to think from the perspective of constructing values from expressions using other values, rather than writing imperative statements to describe how to construct those. In this case, I would try to pick your problem into two parts:
Doing something different when var is set to the empty string vs. other cases.
Doing something different when constructing the first two elements of a list, but only in the non-empty string case.
The first of those sounds like a conditional expression, because you want to choose between two possible outcomes based on a condition:
locals {
example = (
var != "" ?
(expression for normal case) :
(expression for empty case)
)
}
You haven't included details about what ought to happen in the "empty case", but since you suggested you would use a loop in an imperative language I assume that your problem would best map to a for expression in Terraform.
locals {
example = (
var != "" ?
(expression for normal case) :
[ for i in range(10) : (something based on i) ]
)
}
The "normal case" has the extra detail of doing something different based on whether the index is less than two:
locals {
example = (
var != "" ?
[
for i in range(10) : (
i < 2 ?
(something based on i) :
(something else based on i)
)
]
[ for i in range(10) : (something based on i) ]
)
}
(Your original question used i < 2 and i > 2, and my answer above admittedly uses i < 2 and i >= 2 instead; I made an assumption that this interpretation was more likely, but this answer won't work if you really do need to do something special when i == 2 that's different to when it's less than or greater than.)
I can't take this answer any further without more detail about the underlying problem you're trying to solve, but since you seem to be coming from familiarity with Python I do have two general mappings for you that might help you translate from Python-ish approaches to Terraform-ish approaches:
Terraform's conditional operator c ? t : f is equivalent to Python's conditional expressions.
Terraform's for expressions are equivalent to Python's list comprehensions.
In both cases these constructs have similar capabilities but different syntax. If you feel more comfortable exploring in Python first then I'd suggest using these particular Python constructs to build your solution and then you should be able to translate the result to an equivalent Terraform expression.
Terraform is (sadly) not a programming language, so it's not easy (or possible) to convert any pseudo-code to HCL.
Anyway, for your specific case you need to combine two things:
the equivalent of i of your pseudo-code in Terraform would be: count.index (in case you use count)
the if-else-else is probably something like:
(local.a != "" && count.index > 2) ? "option 1" : ((local.a != "" && count.index < 2) ? "option 2" : "option 3" )
(not tested)
Also it might come across as nit-picking, but a tiny remark is "Something to do" in Terraform you rather not declare things to be done (as it's not a procedural programming language), but rather desired state of things.
Option 1:
className={
data.msg.length > 48
? `${classes.message} ${classes.longMessage}`
: `${classes.message}`
}
Option 2:
className={`${classes.message} ${
data.msg.length > 48 ? classes.longMessage : ""
}`}
Is there any performance difference too? Thanks.
They're pretty much the same, and choosing one over the other will have no noticeable effect on the performance of your app. The only thing you will want to consider in this circumstance is the readability - in which case I would argue that the first example would be preferable, but that's entirely subjective.
I totally agree with the readability what #noob mentioned in the answer earlier.
From data side let me configure the following below:
// I assume you are getting this from an API or somewhere else
const data = {
msg: 'Testing text for stackoverflow'
};
From that perspective my suggestion would be to go with the following option:
let messageClassName = `${classes.message}`;
if (data.msg.length > 48) {
messageClassName = `${classes.message} ${classes.longMessage}`;
}
return (
<div className={messageClassName}>
{data.msg}
</div>
)
Most of the time readability helps.
My guess is the top one is faster, here is my reasoning:
Both will have one ternary expression - Where you are using it does not change it's speed, but the commands executed after expression will, and this is what make the first faster, eg:
Case expression TRUE: ( no - difference )
Both will have to concatenate
Case expression FALSE: ( Top one faster )
Top one just evaluate
Botton one concatenate and evaluate
But to do this is too much preciosim.
I am trying to use IF statement in $http.post {} section.
I have written the code as below:
[controller.js]
$http.post("../crud/projects_update.php",{
step_number : $scope.step_number,
//step_one start
if(step_number == 1){ // This is where I get an error.
project_id : $scope.project_data.project_id,
project_title : $scope.project_data.project_title
}
})
.then(function(response){
// do something here
});
However, I get an error on (step_number == 1) part with red underline on '==' part.
I thought it would be working in a simple IF statement form.
Perhaps, I am not using the IF statement in correct comparison syntax..
I have no idea why it is giving me a red line on the '=='.
Does anyone know what could possibly wrong? Please advise me how to fix this error.
Thank you so much in advance!!!
The problem here is that the second parameter for the $http.post function is an object, and you can't use if statements when you are creating an object literal.
That's not an AngularJS thing - it's flat out invalid JavaScript.
There are many possible ways you could do what you're trying to do.
One possible solution is:
$http.post("../crud/projects_update.php",{
step_number : $scope.step_number,
project_id : $scope.step_number === 1 ? $scope.project_data.project_id : undefined,
project_title : $scope.step_number === 1 ? $scope.project_data.project_title : undefined
})
Javascript array value is undefined ... how do I test for that
and
How to check a not-defined variable in JavaScript
these are wrong as far as I'm concerned :
I ONLY get :
when trying to :
console.log(!fromToParameters[7].value.firstInput);
console.log(!!fromToParameters[7].value.firstInput);
console.log(fromToParameters[7].value.firstInput === undefined);
console.log(typeof fromToParameters[7].value.firstInput == 'undefined');
console.log(fromToParameters[7].value.firstInput !== undefined);
console.log(typeof fromToParameters[7].value.firstInput != 'undefined');
but this (the entry exists) works fine :
console.log(!fromToParameters[0].value.firstInput);
console.log(!!fromToParameters[0].value.firstInput);
console.log(fromToParameters[0].value.firstInput === undefined);
console.log(typeof fromToParameters[0].value.firstInput == 'undefined');
console.log(fromToParameters[0].value.firstInput !== undefined);
console.log(typeof fromToParameters[0].value.firstInput != 'undefined');
false
true
false
true
false
true
is it a question of react being different from js? why can't I do the same as in these stackoverflow threads?
UPDATE :
so you cannot point to a missing array element at all.
Check answers below.
I think I'll be using an array.lenght stored in a const that I then check my increment against it within my "for" loop to allow or disallow modifying my array entries on a case-by-case basis.
it's really annoying that you can't just ask js if a damn var of an unexisting array index exists or not.
it seems like this would be straightforward stuff : can't point to the index? then NO. no not this variable nor any other variable exists at this index. end of.
the guys at js definitely should put in a note for adding something as simple as this.
I'm tempted to post my code as I have something that allows for me to do what I want (calling a index with lots of undefineds and getting a object with ""s instead) but it's a bit monstrous.
You should do like this:
console.log(fromToParameters[7] && fromToParameters[7].value.firstInput);
console.log(!!fromToParameters[7] && fromToParameters[7].value.firstInput);
console.log( fromToParameters[7]&& typeof fromToParameters[7].value.firstInput == 'undefined');
I have just added check. So if fromToParameters[7] is undefined or null, your code will not break.
Check for the index first (ex: console.log(!yourArray[x]), then depending if that test passes or fails, access/add the indexes/properties you want.
I would like to use Lua not only for writing scripts in separate files, but I would also like to embed Lua expressions into configuration properties.
For example :
{
"MyConfigProperty" : "Hello mister {{ app:getUserName() }} !"
}
Here app:getUserName() would return a string and the the whole script contained in {{ }}, which is actually just an expression, could be evaluated to a string value.
How could I "call" this piece of Lua code so that it is evaluated and leaves a string value on the stack ?
One solution could be to wrap it into a "return (" + expression + ")" as suggested in the Lua documentation.
Running this should (I guess) leave the resulting string on the stack as a return value. But this could be a problem because then I would not be able to determine when a string was successfully returned and when an error message was returned do to a failure in the execution.
How could I do that using the C API without losing the capability of detecting errors ? Thank you.
EDIT : Another solution could be to prefix with return nil, and then check the type of the first return value. If the first return value is nil, get the successfully evaluated value which is the second return value. Else if the first return value is a string, an error occured. There might have been a cleaner approach though.