0

I am working in an angular js demo project where i am using UI Router for routing. In my project there are two views named view1 and view2. For navigation i used tab design so that user can navigate from one view to another. In view1 user needs to insert his email address and password and after clicking submit button i store this information in mysql database by using http service of angular. After that when i clicked view2 it shows all user's email address and password and working fine. But if i return to view1 and register another user and go back to view2 then this new user information is not updated and previous data showed. For showing user information from database i used php.

So what i am thinking is that, at first when we clicked view2 , the page request go to the server and user information show in the view2 after coming from server it turns into plain html. After that when we added new data in mysql database and press view2 the request does not go to server as a result i did not get the newly added user information.

I don't know i am right or wrong but i need to solve this problem. Please help me. How can i find updated data from mysql database.

You can check this problem from here [http://www.iamcreative.comlu.com/angular/][1]

Here is my view1.php Code

<div  ng-controller="Controller1">

   <form role="form">
    <div class="form-group">
        <label for="email">Email address:</label>
        <input type="email" class="form-control" id="email">
    </div>
    <div class="form-group">
        <label for="pwd">Password:</label>
        <input type="password" class="form-control" id="pwd">
    </div>

    <button ng-click="submitForm();"type="submit" class="btn btn-default">Submit</button>

       <div class="form-group">
           <label id="user-status"></label>

       </div>
</form>
</div>

Here is my view2.php code

<?php
include '../server_side/database_lib/DbHelper.php';
$db=new DbHelper();
$result=$db-> selectAllRow(Constants::$TABLE_USER_INFO,Constants::$USER_INFO_ID, 'DESC');
?>
<div class="container" ng-controller="Controller2">

    <div class="table-responsive">          
        <table class="table">
            <thead>
                <tr>
                    <th>Email</th>
                    <th>Password</th>
                </tr>
            </thead>
            <tbody>

               <?php
                foreach($result['data'] as $value)
                    {
                          echo "<tr>";
                        echo "<td>".$value["email"]."</td>"   ;
                        echo "<td>".$value["password"]."</td>"  ; 
                         echo "<tr/>";
                    }

                ?>
              </tbody>
        </table>
    </div>
  </div>

Here is my controller code

angular.module('myApp.controllers', []).controller('MainController',function($rootScope,$scope,$location,$state){
    $scope.navigate=function(sub)
    {
         if(sub=="view1")
        {
        $state.go('view1');
         }
        else if(sub=="view2"){

          $state.go('view2');  

        }

    }
}).controller('Controller1',function($rootScope,$scope,$location,$state,regService){


  $scope.submitForm=function(){
      var email=  document.getElementById("email").value;
     var password=   document.getElementById("pwd").value;


        if(password!="" && email !="")
        {


            var data={"email":email,"password":password};
            var promise =  regService.getRegistration(data);
            promise.then(
                function success(payload) { 

                       var status = payload.data.status;
                    if(status==2)
                    {
                        document.getElementById("user-status").innerHTML=payload.data.msg;
                    }
                    else if(status==0)
                    {
                        document.getElementById("user-status").innerHTML=payload.data.msg;
                    }
                    else if(status==1)
                    {
                        document.getElementById("user-status").innerHTML="Registration Successful";
                    }

                     },
                function error(errorResponse) {
                    //alert('Error connecting server'+ errorResponse);
                    alert("Error")
                }
            );

        }
        else{

            document.getElementById("user-status").innerHTML="*Please insert all the field";

        }

    }
 }).controller('Controller2',function($rootScope,$scope,$location,$state){

});

and index.html code like this

 <section>

       <div class="container-fluid">
       <div class="row">

           <div class="col-md-12">

        <div class="tab-bar">  
                   <ul>

                   <li><a class="tab" ng-click="navigate('view1');" >View 1</a></li>
                       <li><a class="tab" ng-click="navigate('view2');" >View 2</a></li>


                   </ul>
                   </div>

           </div>
          </div>

       <div class="row">

           <div class="col-md-12">
               <div ui-view ></div>

           </div>

       </div>
       </div>
   </section>

Please help me what can i do?

  • where is the ui-view tag in your views ? You are using UI router $state.go but i cant see the ui-view tags , are you missing some code ? – Pratik Aug 07 '15 at 10:03
  • @Pat Check my question now. I edited this and also include that code. Now please help me – kazi sifat Aug 07 '15 at 10:28

1 Answers1

1

This code has so many things which are not the best practices when using Angular.

I would recommend you do some reading on how to use angualr in your project.

I will mention a few things which I can see straight away , but this list is not exhaustive , you need to read up on how angular works and its best practices ..

  1. You are using javascript to manipulate the content in your page , this is generally not a good idea , you should be letting angular handle the manipulations of the DOM as much as possible.

document.getElementById("user-status").innerHTML=payload.data.msg;

  1. You have put server side code in angular ui-templates . This is not how you use templates , By default angular will only load the template ones and cache it , any subsequent requests for the template will be served from the cache. Server side requests should always be in angular services.

    1. This is a minor issue compared to the others but you do not need the ng-controller tags when using UI view , the controller to be triggered should be mentioned in the ui-view configuration , this will cause the backend to be called twice once issue #2 is corrected.

For your specific problem , you will be able to resolve it when you correct issue #2 and move it into angular code instead of template.

However as I mentioned , please read up on how angular should be used and what are the best practices.

Pratik
  • 868
  • 1
  • 9
  • 24
  • Sorry i can not accept your answer because first of all document.getElementById("user-status").innerHTML=payload.data.msg; is not goog practice for angular i know. This is not my concern in this example and i also explained my thinking in my question that the page is loading from server only once. Again i also know that it is best practice to fetch data using service , factory or provider. – kazi sifat Aug 07 '15 at 13:02
  • But my point is if i want to do this like my way (php) then what can i do or how can i do because for session management and other issue i prefer php so if i could do this then it will be a great help for me. According to your reply this is not possible right? – kazi sifat Aug 07 '15 at 13:03
  • @kazisifat : You need to resolve issue #2 to solve your problem , the server side code is not getting called the second time because view code is treated as text templates which are cached by angular . So when you access the page the first time , it makes a http request to the server , on the second instance it just uses the cached text of the first request as template. You could try clearing the template cache by using the code mentioned here : http://stackoverflow.com/a/27432167/2794980 which will make sure that requests for templates are made each time you call $state.go – Pratik Aug 07 '15 at 15:08