2016-03-28 12 views
2

Tablo sütunlarımı gizlemek için angularjs $ yayın özelliğini kullanmaya çalışıyorum. Veri yayınlanırsa, tablo sütunu gösterilmeli, tablo sütunu gizlenmelidir. Şu anda kodumu çalıştırırsam, giriş yaptıktan sonra oturum açma kullanıcı adımı işleve yayınladım ama hala tüm masam göstermiyor. Çözmenin bir yolunu biliyor olabilirim.

Benim controller.js kodudur:

<table class="table table-hover table-bordered"> 
<thead> 
    <tr> 
     <th>Title</th> 
     <th>Description</th> 
     <th ng-show="noteEnabled">Notes</th> 
    </tr> 
</thead> 
<tbody> 
    <tr ng-repeat="movie in movies | pagination: currentPage * entryLimit | 
    limitTo: entryLimit"> 
     <td> 
      {{movie.title}} 
     </td> 
     <td> 
      {{movie.description}} 
     </td> 
     <td data-ng-click="selectFilmDetails($event,movie)" ng-show="noteEnabled" > 
      {{movie.comment}} 
     </td> 
    </tr> 
</tbody> 
</table> 

Bu yayınlanan kontrol kodu:

.controller('LoginController', 
      [ 
       '$scope', 
       'dataService', 
       '$location', 
       '$window', 
       '$rootScope', 
       function ($scope, dataService, $location, $window, $rootScope){ 
        $scope.check_login = function($event,userID,passwd){ 
         dataService.login(userID,passwd).then(
          function (response){ 
           if(response.result.status=='ok'){ 
            $scope.user = response.user; 
            $rootScope.$broadcast("passuser", $scope.user); 
            $location.path('#/home'); 
            //$window.location.reload(); 

           }else{ 
            $scope.message = response.result.message; 

           } 
          }, 

          function (err) { 
           $scope.status = 'unable to connect to data' + err; 
          } 
         ); 

        }//end of function check_login 
       } 
      ] 
     ) 

Daha önce bu zaman kısmi HTML kodu

.controller('AllMovieController', 
      [ 
       '$scope', 
       'dataService', 
       '$location', 
       '$rootScope', 

       function ($scope, dataService, $location, $rootScope){ 
        $scope.noteEnabled = false; 
        $scope.movies = [ ]; 
        $scope.movieCount = 0; 
        $scope.currentPage = 0; //current page 
        $scope.entryLimit = 20; //max no of items to display in a page 

        var getAllMovie = function() { 
         dataService.getAllMovie().then(
          function (response) { 
           $scope.$on("passuser", function ($event, data){ 
            if(data){ 
             $scope.movies = response.data; 
             $scope.showSuccessMessage = true; 
             $scope.successMessage = "All movie Success"; 
             $scope.noteEnabled = true; 
            }else{ 
             $scope.movies = response.data; 
             $scope.noteEnabled = false; 
            } 
           }); 


          }, 
          function (err){ 
           $scope.status = 'Unable to load data ' + err; 
          } 
         ); // end of getStudents().then 
        }; 

        $scope.numberOfPages = function(){ 
         return Math.ceil($scope.movies.length/$scope.entryLimit); 
        }; 


        getAllMovie(); 

       } 
      ] 
     ) 

olan

kullanıcının oturum açıp açmadığını kontrol etmek için kullanılan oturum, ancak şimdi kullanıcı adıma contro geçmek için yayın kullanıyorum ller. Ve aynı ben bu denetleyiciye kullanıcı adını geçmeye çalıştı çalışmaz. Bu konuda gerçekten yardıma ihtiyacım var. Şimdiden teşekkürler.

cevap

1

Olay dinleyicinizi, getAllMovie() servis çağrınızın .then() dışına taşımanızı tavsiye ederim. Söz vermeden önce passuser etkinliği yayınlanırsa ne olur?

Güncelleme: Eğer yayınlayan vermesiyle mesele olay dinleyicisi Denetleyiciniz örneği değil olabilir İşte kodunuzu yeniden öneriyoruz nasıl (Ben enjekte ama kullanarak değildi modülleri kaldırıldı) olay. Bu bir tahmindir çünkü bunların tek bir görünüm, farklı görünümler vb. Olup olmadığı belirsizdir. Oturum açmış durumun bunun yerine bir değerde saklanmasını öneririm. Bu sadece bir örnektir - en iyi yol olmayabilir ya da ihtiyacınız olan her şeyi ele alacaktır. Bunu test etmedim, böylece istediğin gibi çalışmasını sağlamak için onunla oynamak zorunda olabilirsin. İşte benim güncellenmiş önerdiğimiz kod:

.value('UserInfo', { user: '', loggedIn: false }) 
.controller('LoginController', 
    ['$scope', 'dataService', '$location', 'UserInfo', 
    function ($scope, dataService, $location, UserInfo) { 
     $scope.check_login = function($event,userID,passwd) { 
      dataService.login(userID,passwd).then(
       function (response){ 
        if(response.result.status=='ok'){ 
         UserInfo.user = response.user; 
         UserInfo.loggedIn = true; 
         $location.path('#/home'); 
        } else { 
         $scope.message = response.result.message; 
         UserInfo.user = ''; 
         UserInfo.loggedIn = false; 
        } 
       }, 
       function (err) { 
        $scope.status = 'unable to connect to data' + err; 
        UserInfo.user = ''; 
        UserInfo.loggedIn = false; 
       }); 
     }//end of function check_login 
    }]) 
.controller('AllMovieController', ['$scope', 'dataService', 'UserInfo', 
    function ($scope, dataService, UserInfo) { 
     $scope.noteEnabled = false; 
     $scope.movies = []; 
     $scope.movieCount = 0; 
     $scope.currentPage = 0; //current page 
     $scope.entryLimit = 20; //max no of items to display in a page 

     $scope.noteEnabled = UserInfo.loggedIn; 

     var getAllMovie = function() { 
      dataService.getAllMovie().then(
       function (response) { 
        $scope.movies = response.data; 
        $scope.showSuccessMessage = true; 
        $scope.successMessage = "All movie Success"; 
       }, 
       function (err) { 
        $scope.status = 'Unable to load data ' + err; 
       }); 
     }; 

     $scope.numberOfPages = function() { 
      return Math.ceil($scope.movies.length/$scope.entryLimit); 
     }; 

     getAllMovie(); 
}]); 
+0

Hala benim için çalışmıyor. Verileri günlüğe kaydetmeyi denedim ama hiçbir değer elde edemiyorum. Onun scope.on işlevine bile girmiyor – anonymous5671

+0

Sorunuzu, $ rootScope. $ Broadcast() 'olarak adlandırdığınız yere dahil etmek için günceller misiniz? Kodun biraz çalıştığından emin olmak için hata ayıkladı mı? – Lex

+0

Yayınlanmış denetleyiciyi ekledim, bir göz atın – anonymous5671

İlgili konular