Solved

AngularJS Nested If Statement

Posted on 2014-10-23
5
433 Views
Last Modified: 2014-10-23
I'm trying to add a bit of logic to my controller with a couple of nested If statements. By themselves they work fine, but after nesting them, i'm not getting any results.

This is a working statement:
            if (typeof object["Frequency"]!='undefined' && object["Frequency"]=='yearly' && ('now' <= 'upcoming')) {
            $scope.summary[segment].totalLateRelationships++;
            $scope.summary[segment].lateRelationships.push(object);
        } 

Open in new window


This is working:
        if (!(object["nextmeetingowner"].length) || !(object["Last Meeting Date"].length) ) {
            $scope.summary[segment].totalLateRelationships++;
            $scope.summary[segment].lateRelationships.push(object);
        }

Open in new window


This is what I'm trying to accomplish:
            if (!(object["primaryaccountability"].length) || (!(object["nextmeetingowner"].length))) {
          if  (typeof object["Frequency"]!='undefined' && object["Frequency"]=='yearly' && ('now' <= 'upcoming'))
            {
                $scope.summary[segment].totalLateRelationships++;
                $scope.summary[segment].lateRelationships.push(object);
            }
        }

Open in new window

0
Comment
Question by:brendanlefavre
  • 2
  • 2
5 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40400899
The two seperate statements are not the same as you have merged.

if (!(object["nextmeetingowner"].length) || !(object["Last Meeting Date"].length) ) {

if (!(object["primaryaccountability"].length) || (!(object["nextmeetingowner"].length))) {
0
 

Author Comment

by:brendanlefavre
ID: 40400932
this should be the correct one

                if (!(object["nextmeetingowner"].length) || !(object["nextmeetingowner"].length) ) {
                    $scope.summary[segment].totalLateRelationships++;
                    $scope.summary[segment].lateRelationships.push(object);
                }

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 40400937
What is your real original code.
The above has the same variable in the condition
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40400945
var frequencyIsNotUndefined = typeof object["Frequency"] != 'undefined';
var frequencyIsSetToYearly = object["Frequency"] == 'yearly'; 
var nowIsBeforeUpcoming = 'now' <= 'upcoming'; // ??? ALWAYS TRUE
var nextmeetingownerIsFilled = !object["nextmeetingowner"].length;
var LastMeetingDateIsFilled = !object["Last Meeting Date"].length;
if( frequencyIsNotUndefined && frequencyIsSetToYearly && nowIsBeforeUpcoming && nextmeetingownerIsFilled && LastMeetingDateIsFilled ) {
            $scope.summary[segment].totalLateRelationships++;
            $scope.summary[segment].lateRelationships.push(object);
} 

Open in new window


http://jscompress.com/
https://www.npmjs.org/package/gulp-uglify
http://gulpjs.com/
0
 

Author Closing Comment

by:brendanlefavre
ID: 40400964
This solution makes much more sense than the approach I was using
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now