Routing trouble in Angular & NodeJS [angular]

ε祈祈猫儿з 提交于 2019-12-05 22:54:40

User2401192,

First things first you need to declare ng-view in your html :

    <div ng-view></div>

Now when your templateUrl changes ( e.g. you execute $location.path('home').replace(); ),

$routeProvider.when('/angular/home', {
    templateUrl: 'view/home.html',
    controller : 'homeCtrl'
});

your ng-view is replaced with the content of the new url. In this case view/home.html.

Two things I'd like to add :

  1. Don't use the classic $.ajax(..), use $http service, because it has the nice advantages of promise APIs, and the extremely useful http interceptors ( might not sound like much but they are - e.g. showing a loading splash or intercepting 403 Unauthorized requests.
  2. Nothing :), there was just one thing actually.

Replace app.js code

var app = angular.module('myApp', []);
app.config(['$routeProvider','$locationProvider',function($routeProvider, $locationProvider) {
  $locationProvider.html5Mode(true);
  $routeProvider.when('/angular/home', {
    templateUrl: 'view/home.html',
    controller : 'homeCtrl'
  });
}]);
function homeCtrl($scope){
  alert('someone call me');
}
app.controller('ajaxCtrl', function($scope, $window) {
  var url = "http://localhost:7788/login";
  $scope.submitPost = function() {
    $.ajax({
      url: url,
      type: 'POST',
      dataType: 'json',
      data: {username: $scope.username, password: $scope.password},
      success: function(data) {
        $scope.$apply(function(){
        $scope.result=data.message;
          if (data.status === 'true') {
            alert($location.path());
            //$location.path('home').replace();
            return $window.location.href = 'home.html';
           }
        });
      },
   error: function(data) {
     alert('Error get data from server');
   }
  });
 };
});

I will try it's working.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!