简体   繁体   English

角度水疗应用程序无法正常工作ngResource

[英]angular spa app not working ngResource

In Angular ngResource is not functionning correctly. 在Angular中,ngResource无法正常运行。 I have added the script reference. 我添加了脚本参考。 The problem is that when I add the dependencie to ngResourc blank pages are displayed. 问题是,当我将依赖项添加到ngResourc时,将显示空白页面。 What should I do? 我该怎么办?

/*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 = {};
        });    
    };    
}]);

I will add code on demand 我将按需添加代码

It's always better in such cases to open a debug window (F12 or command+shift+I), toggle Console tab and check what it says. 在这种情况下,最好打开调试窗口(F12或Command + Shift + I),切换“控制台”选项卡并检查其内容。 Blank page can de displayed within several circumstances. 在某些情况下可以显示空白页。

Also you can try to complete the commented-out version of your code to avoid new module declaration like this^ var app = angular.module('myApp', ['ngResource']); 您也可以尝试完成代码的注释版本,以避免新的模块声明,例如^ var app = angular.module('myApp',['ngResource']);

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

app.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 = {};
        });

    };

}]);

app.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 = '';
    });
  }*/






}]);

app.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 = {};
        });

    };

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

    $scope.logout = function () {

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

you havent add the resource to your function 您没有将资源添加到您的功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM