I have a annidate variable like that.
$scope.a = { b: {c : 1} };
var test = $scope.a.b.c;
// test == 1
$scope.a = {}
var test = $scope.a.b.c;
// ERROR
I want test variable will be null or undefined.
How can I fill test variable without error?
some advice?
I'm looking for a smart way
not only
if(angular.isDefinied($scope.a) && angular.isDefinied($scope.a.b) && angular.isDefinied($scope.a.b.c))
You probably want to do something like that:
var test = ($scope.a && $scope.a.b && $scope.a.b.c) ? $scope.a.b.c : undefined;
If you are not defined the value before using it, then definitely, it will throw an error. You must define or you must check whether it is defined or not. Otherwise you can't resolve the error. var test will get a value only if there is no error. Now, its up to you to decide and use a smart way
var test = $scope.a?((typeof($scope.a.b)=="object" && $scope.a.b.c)?$scope.a.b.c:(typeof($scope.a)=="object" && $scope.a.b)?$scope.a.b:null):null
Related
I am using a if condition in angularjs to check is a status is != Delivered or status is != Canceled then alert a message
if($scope.itemDeliveredStatus != "Canceled" || $scop.itemDeliveredStatus != "Delivered"))
{
alert("Please set item status to Delivered! or Canceled!");
return false;
}
Typo on the second condition, you put $scop instead of $scope :
$scop.itemDeliveredStatus
and an extra ) at the end of that line which is not necessary
First you do have a typo, but that is only one small issue. The other is a logic problem.
Your logic is wrong with the use of OR. Simple example below shows you that.
var foo = "A";
if (foo !== "A" || foo !== "B") {
console.log("WHY?")
}
Why? Because you are saying that in order for this if statement to be true, the variable foo has to be equal to two things.
You want it to say if foo does not equal A AND foo does not equal B, than show error.
var foo = "A";
if (foo !== "A" && foo !== "B") {
console.log("WHY?")
} else {
console.log("Better!!!")
}
So with the two issues fixed, your code would look like:
if ($scope.itemDeliveredStatus != "Canceled" && $scope.itemDeliveredStatus != "Delivered") { /*.. code */ }
how can i check if any javaScript object's property exists and if it exists then it has a valid value?
actually,i am a beginner and trying to solve this-
Check if the second argument is truthy on all objects of the first argument(which is an array of objects).i.e.
check if the second argument exists in all the objects in first argument(an array) as a property.
if it exists, it should not be-
invalid, as age can't be 0
null
undefined
empty string('')
NaN
till now i have this-
function truthCheck(collection, pre) {
for(var i=0;i<collection.length;i++){
if(!(pre in collection[i])||collection[i]
[pre]===undefined||isNaN(collection[i]
[pre])||collection[i][pre]===""||
collection[i][pre]===null||collection[i]
[pre]===0)
{
return false;
}
}
return true;
}
i know this is not the best wayto solve .Is there a better way to do this?i don't like that long if statement in my code.i have seen other SO links-link1,link2 but none of them seemed to solve my query. any kind of help is highly appreciated.
P.S. this code is not working for some true cases even.
o = new Object();
o.prop = 'exist';
if(o.hasOwnProperty('prop')){
if(o['prop']){
alert('good value')
}
}
https://stackoverflow.com/a/6003920/1074179
this is what i was looking for and absolutely logical-
for(var i in array){
if((prop in array[i])&& Boolean(array[i][prop]))
{
//do something
}
}
the Boolean() function is something which made my day. Learn more at this link.
Look at the below example.
let the json object be
var a = { obj1:"a",obj2:"b"}
to check if an object exists,you can use hasOwnProperty() method.
a.hasOwnProperty("obj2") //this will return true
a.hasOwnProperty("obj3") // this will return false
to check the value of an object
if(a["obj1"] && a["obj1"]!="" && a["obj"]!=0){
//place your logic here
}
I am trying to use $parse to get my required result.
Here is my Example. Check script.js file
I want to parse an expression but I am getting error. How can I solve this kind of data?
Here is my code
scope.data = {name:{_en:"username", _ar:'مفقود '}}
// set variable in scope like scope.name_en = scope.data._en, scope.name_ar = scope.data._ar
for(var i in scope.data) for(var j in scope.data[i]) scope[i+j] = scope.data[i][j];
scope.messages2 = [{code:200, msg_en:"{{name_en}} is missing.", msg_ar:"مفقود {{name_ar}}"}];
scope.finalMegs = [];
for( var i in scope.messages2) {
var obj = {};
for(var j in scope.messages2[i]){
if(j == 'code') continue;
console.log(scope.$eval(scope.messages2[i][j]) );
obj[j] = $parse(scope.messages2[i][j])
}
/*
required object is {msg_en = "username is missing.", msg_ar:"مفقود مفقود "}
*/
scope.finalMegs.push(obj);
}
console.log(scope.finalMegs);
Thank you.
Syntax of your messages is incorrect, so the messages can't be parsed by $parse or $eval. You should write them in this format:
scope.messages2 = [
{
code:200,
msg_en:"name_en + ' is missing.'",
msg_ar:"'مفقود ' + name_ar"
}
];
Then if you want to get object with already formatted messages you should call $parse function in this way:
obj[j] = $parse(scope.messages2[i][j])(scope);
Because $parse converts angular expression to function, which you should call with your scope to get expected results.
But in your case, I think that you can replace those line to this also:
obj[j] = scope.$eval(scope.messages2[i][j]);
Result will be the same.
Example on plunker. (I've reformatted your example a little bit)
Please, take a look also at the example of usage scope.$eval here and documentation about $parse.
I've tried:
if(angular.isUndefined(value)){
// something
}
and
if(!(value)){
// something
}
Is there a difference between the two?
Is there a use-case to choose one instead of the other?
var foo = false;
if(!foo) {
// will log
console.log("foo is defined but false");
}
if(angular.isUndefined(foo)){
// will not log as foo is defined
console.log("foo is undefined")
}
another example without define foo
if(!foo) {
// will throw exception "Uncaught ReferenceError: foo is not defined "
console.log("foo is defined but false");
}
if(angular.isUndefined(foo)){
// will log
console.log("foo is undefined")
}
so effective angular.isUndefined(foo) does nothing else than evaluating
if(typeof foo == "undefined")
wrapped for saving 1 character yeah.
while !-operator checks if a defined variable evaluates to false
so
if(!foo)
is the same like
if( foo != true)
UPDATE:
As stated in comments, when i write "evaluates to false" there is false null undefined NaN ""(empty string) and 0 included
! is the logical not operator in JavaScript while angular.isUndefined(value) checks if the reference is undefined
Which one to use completely depends on what you are trying to do in the end.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators and https://docs.angularjs.org/api/ng/function/angular.isUndefined
This is callParent method in ExtJS. What are $previous, $owner, and $name? Why is there a leading "$" sign?
callParent: function(args) {
var method;
// This code is intentionally inlined for the least number of debugger stepping
return (method = this.callParent.caller) && (method.$previous ||
((method = method.$owner ? method : method.caller) &&
method.$owner.superclass.$class[method.$name])).apply(this, args || noArgs);
}
It's internal detail the class system sets up so it can track methods. The $ are there so it's less likely to conflict with your own property names on the class.