2016-11-16 3 views
0

Das ist mein app.jsWarum Controller zweimal aufgerufen, wenn sich der Status ändert?

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', 

      }) 
    }); 

Wenn ich ui-sref="dashboard.employees" Controller klicken Sie zweimal aufruft.

calls twice

Dies ist mein Controller, die ich für alle Ansichten verwenden möchten. Ich entwickle ein cms auf laravek und eckig. Ich kann keinen neuen Controller für jede Tabellenentität erstellen.

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; 
     } 
    } 
}); 

Wo ist mein Fehler? Wie kann ich das beheben?

+0

reden Sie beim Laden ?? –

+0

nein, wenn ich zwischen den Zuständen wechsle – Quasar

+0

Ihr Bild zeigt nicht an, dass Ihr Controller zweimal aufgerufen wird, sondern dass zweimal von Ihrem Controller ein API-Aufruf erfolgt. Bitte geben Sie Ihren Controller-Code an. – devqon

Antwort

0

freundlich überprüfen, wenn Sie die Steuerung in Ihrer employee.tmpl.html Seite genannt werden, wie ng-controller="employeesController"

Bitte es entfernen, wenn Sie die controller rufen in Ihrem html

+0

nein, nur für Elternvorlage – Quasar

+0

Wenn du 'ng-controller =" employeesController "' irgendwo verwendest, entferne es einfach –

+0

oh, danke) jetzt, nur eine Anfrage) – Quasar

Verwandte Themen