2016-11-16 88 views
0

這是我app.js爲什麼控制器在狀態改變時調用兩次?

angular.module('app', ['ui.router', 'satellizer']) 
    .constant('API_URL', 'http://localhost/angular/public/api/v1/') 
    .config(function($stateProvider, $urlRouterProvider, $authProvider) { 
     $authProvider.loginUrl = 'angular/public/api/authenticate'; 
     $urlRouterProvider.otherwise('/auth'); 
     $stateProvider 
      .state('auth', { 
       url: '/auth', 
       templateUrl: 'app/view/login.html', 
       controller: 'AuthController as auth' 
      }) 
      .state('dashboard', { 
       url: '/dashboard', 
       templateUrl: 'app/view/dashboard.tmpl.html', 
       params: { 
        model: '' 
       } 
      }) 
      .state('dashboard.employees', { 
       templateUrl: 'app/view/employee.tmpl.html', 
       controller: 'employeesController', 

      }).state('dashboard.test', { 

       templateUrl: 'app/view/edit.tmpl.html', 
       controller: 'employeesController', 

      }) 
    }); 

當我點擊ui-sref="dashboard.employees"控制器調用兩次。

calls twice

這是我的控制,我想使用的所有意見。我在laravek和angular上開發了一個cms。我無法爲每個表實體創建新的控制器。

angular.module('app') 
    .controller('employeesController', function($scope, $http, API_URL,$stateParams) { 
     //retrieve employees listing from API 
     $scope.employees = ''; 


    $http.get(API_URL + $stateParams.model) 
     .success(function(response) { 

      $scope.employees = response; 
     }); 
    //show modal form 
    $scope.toggle = function(modalstate, id) { 
     $scope.modalstate = modalstate; 

     switch (modalstate) { 
      case 'add': 
       $scope.form_title = "Add New Employee"; 
       break; 
      case 'edit': 
       $scope.form_title = "Employee Detail"; 
       $scope.id = id; 
       $http.get(API_URL + $stateParams.model+'/' + id) 
        .success(function(response) { 
         console.log(response); 
         $scope.employee = response; 
        }); 
       break; 
      default: 
       break; 
     } 

     $('#myModal').modal('show'); 
    } 

    //save new record/update existing record 
    $scope.save = function(modalstate, id) { 
     var url = API_URL + "employees"; 

     //append employee id to the URL if the form is in edit mode 
     if (modalstate === 'edit') { 
      url += "/" + id; 
     } 
     console.log('saved'); 
     $http({ 
      method: 'POST', 
      url: url, 
      data: $.param($scope.employee), 
      headers: { 
       'Content-Type': 'application/x-www-form-urlencoded' 
      } 
     }).success(function(response) { 
      var index = _.findIndex($scope.employees, function(b) { 
       return b.id == $scope.employee.id; 
      }); 
       console.log(index); 
      if (index != -1) { 
       $scope.employees[index] = $scope.employee; 
      } else { 

       console.log($scope.employee); 
       $scope.employee.id = response; 
       $scope.employees.push($scope.employee); 
       console.log($scope.employees); 
      } 
      $('#myModal').modal('toggle'); 

     }).error(function(response) { 
      console.log(response); 
      alert('This is embarassing. An error has occured. Please check the log for details'); 
     }); 
    } 

    //delete record 
    $scope.confirmDelete = function(employee) { 
     var isConfirmDelete = confirm('Are you sure you want this record?'); 
     if (isConfirmDelete) { 
      $http({ 
       method: 'DELETE', 
       url: API_URL + 'employees/' + employee.id 
      }). 
      success(function(data) { 
       _.remove($scope.employees, function(n) { 
        return n.id == employee.id; 
       }); 
       console.log(data); 
      }). 
      error(function(data) { 
       console.log(data); 
       alert('Unable to delete'); 
      }); 
     } else { 
      return false; 
     } 
    } 
}); 

我的錯誤在哪裏?我該如何解決這個問題?

+0

你在說什麼,而加載? –

+0

不,當我在狀態之間移動 – Quasar

+0

您的圖像沒有顯示您的控制器被調用了兩次,但是您的控制器通過兩次api調用。請包含您的控制器代碼。 – devqon

回答

0

查收,如果你被稱爲控制器在employee.tmpl.html頁,像ng-controller="employeesController"

刪除它,如果你調用controllerhtml

+0

不,只在父模板上 – Quasar

+0

如果你在任何地方使用'ng-controller =「employeesController」',請刪除它 –

+0

哦,謝謝)現在只有一個請求) – Quasar

相關問題