2016-10-25 54 views
0

我遇到了ng ngResource的問題。當我添加不填充頁面的相關性。這裏是我的代碼:ngResource和SPA angularjs不顯示

/*var app = angular.module('myApp', ['ngResource']); 

app.factory('todoFactory', function($resource) { 
    return $resource('/api/meetups'); 
});*/ 

angular.module('myApp').controller('loginController', 
    ['$scope', '$location', 'AuthService', 
    function ($scope, $location, AuthService) { 

    $scope.login = function() { 

     // initial values 
     $scope.error = false; 
     $scope.disabled = true; 

     // call login from service 
     AuthService.login($scope.loginForm.username, $scope.loginForm.password) 
     // handle success 
     .then(function() { 
      $location.path('/'); 
      $scope.disabled = false; 
      $scope.loginForm = {}; 
     }) 
     // handle error 
     .catch(function() { 
      $scope.error = true; 
      $scope.errorMessage = "Invalid username and/or password"; 
      $scope.disabled = false; 
      $scope.loginForm = {}; 
     });  
    };  
}]); 

angular.module('myApp').controller('logoutController', 
    ['$scope', '$location', 'AuthService', '$resource', 
    function ($scope, $location, AuthService) { 

    $scope.logout = function() { 

     // call logout from service 
     AuthService.logout() 
     .then(function() { 
      $location.path('/login'); 
     });  
    }; 

/* 

$scope.posts = []; 
    $scope.newPost = {created_by: '', text: '', create_at: ''}; 

    $scope.afficher = function(){ 
     $scope.newPost.created_at = Date.now(); 
     $scope.posts.push($scope.newPost); 
     $scope.newPost = {created_by: '', text: '', created_at: ''}; 
    };  
*/ 
    $scope.meetups = []; 
    /*var Meetup = $resource('/api/meetups'); 

    Meetup.query(function (results) { 
    $scope.meetups = results; 
    }); 

    $scope.meetups = [] 

    $scope.createMeetup = function() { 
    var meetup = new Meetup(); 
    meetup.name = $scope.meetupName; 
    meetup.$save(function (result) { 
     $scope.meetups.push(result); 
     $scope.meetupName = ''; 
    }); 
    }*/ 
}]); 

angular.module('myApp').controller('registerController', 
    ['$scope', '$location', 'AuthService', 
    function ($scope, $location, AuthService) { 

    $scope.register = function() { 

     // initial values 
     $scope.error = false; 
     $scope.disabled = true; 

     // call register from service 
     AuthService.register($scope.registerForm.username, $scope.registerForm.password) 
     // handle success 
     .then(function() { 
      $location.path('/login'); 
      $scope.disabled = false; 
      $scope.registerForm = {}; 
     }) 
     // handle error 
     .catch(function() { 
      $scope.error = true; 
      $scope.errorMessage = "Something went wrong!"; 
      $scope.disabled = false; 
      $scope.registerForm = {}; 
     });  
    };  
}]); 

當我取消對代碼的第一部分出現問題。我接受任何建議或信息,我真的卡住了,請幫助我。我會按需添加代碼。

回答

0

這是你如何建立你的模塊

angular.module('appName',[/*dependancies*/] 

一旦你創建它,你可以用它通過創建控制器,或指令

angular.('appName').controller(.... or ngular.('appName').directive 

所以首先取消評論第一代碼部分,然後在那裏找到解決方案。並且可能是你沒有在html文件中添加ngResource的腳本引用。 確保您在腳本引用角度後添加它。

+0

10x爲答覆我已添加腳本引用。我取消註釋的第一部分它不顯示任何東西,我沒有得到控制檯上的任何錯誤...請幫助 –

0

$資源在您工廠的註銷控制器&中缺失。

還要確保您的項目/ html中有角度資源js。

var app = angular.module('myApp', ['ngResource']); 

app.factory('todoFactory', ['$resource' , function($resource) { 
    return $resource('/api/meetups'); 
}]); 




    angular.module('myApp').controller('logoutController', 
     ['$scope', '$location', 'AuthService', '$resource', 
     function ($scope, $location, AuthService,$resource) //here. 
{ 

     $scope.logout = function() { 

      // call logout from service 
      AuthService.logout() 
      .then(function() { 
       $location.path('/login'); 
      });  
     }; 
    }