Как мы можем перенаправить на другую страницу с данными объекта, используя AngularJs - PullRequest
0 голосов
/ 22 января 2020

Я пытаюсь перенаправить мою форму на другую страницу, которая является / customerActivation с объектом customer, полученным из маршрута / getCustomerAccount. Я искал в Интернете источники и перепробовал много предложений, но ни одно из них не работает.

Поэтому я хочу спросить, есть ли способ справиться с этим с помощью angular Js? Это моя первая обработка ситуации такого рода

Это моя форма, которая отправляет или вызывает функцию в моем контроллере

<div ng-controller="CustomerController">
  <h3> Please Enter your customer loyalty card number</h3>
  <form ng-submit="activateAccount()">
       <div class="form-group">
            <input type="text" class="form-control" id="formCard"
                   ng-model="nbcard" placeholder="Card number" required>
       </div>

       <button type="submit" class="btn btn-info btn-lg btn-block btn-trans ">
         Activate my account
       </button>
  </form>
</div>

Я реализовал эту функцию в моем контроллере

customerManageAPP = angular.module('customerMngAPP', []);

customerManageAPP.controller('CustomerController',  function($scope, $http) {

    let URL_ACT_ACCOUNT = `http://localhost:${port}/getCustomerAccount`;

    $scope.activateAccount = function() {
        $http.get(URL_ACT_ACCOUNT + `?cardNumber=${$scope.nbcard}`)
            .then(function (response) {
                $scope.customerAccount =  response.data;
                $location.path('/customerActivation/' + $scope.customerAccount.firstname + $scope.customerAccount.lastname);
            });
    };
});

и у меня есть это в моем файле маршрутов

router.get('/customerActivation', function(req, res) {
    res.setHeader('Content-Type', 'text/html');
    res.sendFile( __dirname + '/views' + '/customerActivation.html');
});

router.get('/getCustomerAccount', async (req, res) => {
    const { cardNumber } = req.query;

    const response = await Customers.find({"nb_card": cardNumber});

    res.json(response);

});

1 Ответ

1 голос
/ 23 января 2020

Как правило, для анализа URL используется клиентский маршрутизатор:

С ngRoute:

angular.module("app",["ngRoute"])
.config(function($routeProvider) {
    $routeProvider.when({
        url: "/customerActivation/:customerId",
        templateUrl: "views/customerActivation.html",
        controller: "ActivationController"
    })
})
.controller("ActivationController", function($scope, $routeParams) {
    console.log("customerId", $routeParams.customerId);
})

Для получения дополнительной информации см.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...