-1

I've provided a little snippet that represents this issue below.

var app = angular.module("app", []);

app.controller("AppCtrl", function ($http, $scope) {
  $scope.fetchData = function () {
    document.getElementById("displayBlock").className='hidden';
    document.getElementById("throbber").className='throbber';
    document.getElementById("intermediate").className='unhidden';
    document.getElementById("header").className='step';
    var app = this;
    var p= $scope.formUsernameTex;
    alert(p);
    if(p!="undifined"){
      $http.get("https://api.github.com/users/"+$scope.formUsernameText)
        .success(function (data) {
          app.user = data;

          $scope.name=app.user.name;
          $scope.avatar_url=app.user.avatar_url;
          if (app.user.html_url==null) {$scope.html_url="Not Shared";} else {$scope.html_url=app.user.html_url};
          if (app.user.company==null||app.user.company=="") {$scope.company="Not Shared";} else {$scope.company=app.user.company};
          if (app.user.blog==null) {$scope.website="Not Shared";$scope.websiteLink="";} else {$scope.website=app.user.blog;$scope.websiteLink=$scope.website;};
          if (app.user.location==null) {$scope.location="Not Shared";} else {$scope.location=app.user.location;};
          if (app.user.email==null) {$scope.email="Not Shared";$scope.emailLink="";} else {$scope.email=app.user.email;$scope.emailLink="mailto:"+$scope.email;};
          if (app.user.hireable) {var hireStatus="Yes"} else {var hireStatus="No"};
          $scope.hireable=hireStatus;
          $scope.public_repos=app.user.public_repos;
          $scope.public_gists=app.user.public_gists;
          $scope.followers=app.user.followers;
          $scope.following=app.user.following;
          var date = new Date(app.user.created_at);
          $scope.created_at=date.getDate()+"/"+date.getMonth()+"/"+date.getFullYear();

          //Display Block Functions
          document.getElementById("throbber").className='hidden';
          document.getElementById("displayBlock").className='unhidden';
          document.getElementById("header").className='';
        })
        .error(function () {
          document.getElementById("intermediate").className='hidden';
          document.getElementById("header").className='initial';
          alert("Hmm.... That doesn't look quite right!\n\nOctoStats couldn't find the User's data on GitHub! :-/")
        })
    }
    else {alert("Add name properly");}
  }

})

Suppose here p=undifined I want to give one alert just and rest of the function will not be worked that time.

Please show me how to do it.

Here I can't use if else properly

m59
  • 43,214
  • 14
  • 119
  • 136

2 Answers2

0

The short answer to your question is to change your if statement to this:

if(p != undefined)

where you fix the spelling of "undefined", and also remove the quotes.

The longer answer, like charlietti says, is to really get the hang of the basics of angular before diving into a heavier app. Off the bat, I see a few things that won't make it into your final version:

  1. You'll never (well... super-rarely) use getElementById() in a final angular app. Instead, you'll update your model through your $scope, and let angular change your UI for you. For starters, check out ngShow, ngHide, and ngClass to see how this can be done.
  2. Instead of copying all your app.user properties onto $scope, you would just set $scope.app.user = app.user and let the rest of it work.

I would recommend you check out this SO post to get a better understanding too.

Community
  • 1
  • 1
Hylianpuffball
  • 1,553
  • 10
  • 13
0

I think you may have several typos in either your source code, or your transcription to Stack Overflow.

You are missing a t at the end of $scope.formUsernameTex, you've misspelled undefined, and you don't need to put double quotes around undefined in the following lines:

var p= $scope.formUsernameTex;
alert(p);
if(p!="undifined"){
    $http.get("https://api.github.com/users/"+$scope.formUsernameText)

It should probably be:

var p = $scope.formUsernameText;
alert(p);
if(p != undefined){
    $http.get("https://api.github.com/users/"+$scope.formUsernameText)
Alex Moore
  • 3,415
  • 1
  • 23
  • 39