AngularJS: single page application with string and request form?

It is clear that AngularJS aims to develop SPA (single-page application). However, it is also possible to create an MPA application (multi-page application).

My application is based on several states using Angular libraries like ui-routerand ngResource. First I have a file index.htmlin which I load all the dependencies and ui-view. Then I have three different modules: one main module, which contains the top states, the first home page and loads all the necessary third-party modules. Another module is called Student and has a simple form for POSTsome data in our Django back-end.

The problem lies in the third module: the company module. Here we have two navigation bars: one top navigation bar, on which there is no content yet, and then a sidebar containing several buttons, for example, shown in the image:

enter image description here

At the moment, each of these buttons on the side panel displays the user in a different state, in which the image is loaded into the space where you can see "Welcome ..."

Basically, our states are defined as follows:

'use strict';

talentforce_company
    .config(['$stateProvider', '$urlRouterProvider', 'eehNavigationProvider',
     function($stateProvider, $urlRouterProvider, eehNavigationProvider) {



        // Company area states

        $stateProvider.state('companyState', {
              url: '/company',
              abstract: true,
              //redirectTo: 'homeState',
              template: '<company-area-directive></company-area-directive>'
        });

        $stateProvider.state('homeState', {
              url: '/home',
              parent: 'companyState', 
              controller: ['$scope', '$rootScope', function($scope, $rootScope) {
                    $rootScope.name_filter = "";
                    $rootScope.city_filter = "";
                }
              ],
              template: '<h3>Welcome to TalentForce company area home</h3>'
        });

        $stateProvider.state('select_study_areas', {
            url: '/select_areas',
            parent: 'companyState',
            template: '<study-areas-directive></study-areas-directive>'
          });

        $stateProvider.state('TalentForceState_seeProfile', {
            url: '/profile_list',
            parent: 'companyState',
            // controller: 'People_Controller',
            template: '<people-directive></people-directive>',
          });

          $stateProvider.state('student', { 
            url: '/profile/{personId}',
            parent: 'companyState',
            //templateUrl: 'public/templates/person_template.html',
            controller: 'Person_Controller',
            template: '<profile-directive></profile-directive>',
            resolve: {
              student: function(student_service, $stateParams) {
                  return student_service.getStudents().find(function(student) { 
                    return (student.student_id == $stateParams.personId);
                  });
                }
              }
          });


        eehNavigationProvider
          .menuItem('TF_menu_top.users', {
            text: 'USERNAME',
            iconClass: 'glyphicon-user',
            href: '#'
          });


        eehNavigationProvider
          .menuItem('TF_sidebar.home', {
            text: 'Home',
            state: 'homeState',
            iconClass: 'fa fa-home fa-fw',
          })
          .menuItem('TF_sidebar.profile', {
            text: 'Company Profile',
            href: '#',
            iconClass: 'fa fa-building fa-fw'
          })
          .menuItem('TF_sidebar.students', {
            text: 'Profiles',
            state: 'select_study_areas',
            //href: '/select_areas',
            iconClass: 'fa fa-users fa-fw'
          })
          .menuItem('TF_sidebar.posts', {
            text: 'Job Posts',
            href: '#',
            iconClass: 'fa fa-list-alt fa-fw'
          })
          .menuItem('TF_sidebar.stats', {
            text: 'Statistics',
            href: '#',
            iconClass:'fa fa-bar-chart fa-fw'
          })
          .menuItem('TF_sidebar.universities', {
            text: 'Universities',
            href: '#',
            iconClass:'fa fa-university fa-fw'
          })
          .menuItem('TF_sidebar.tips', {
            text: 'Tips',
            href: '#',
            iconClass: 'fa fa-check-square-o fa-fw'
          })
          .menuItem('TF_sidebar.events', {
            text: 'Events',
            href: '#',
            iconClass: 'fa fa-calendar fa-fw'
          })
          .menuItem('TF_sidebar.help', {
            text: 'Help',
            href: '#',
            iconClass: 'fa fa-question fa-fw'
          })

    }]);

, , , , navbars. . , URL-, , , . .

"", . , . , .

, . , "", , , . URL- - , : oursite.com/offers/?page=1&s=date&s_l=0&s_h=100&t_co=false&t_st=false&hd=false

URL-, . , , ( ), URL , - .

:

<form class="filters" id="myFormId" role="form">

            <div class="col-sm-2" id="filter-column">
                <div class="controls">
                    <select id="country" name="country">
                        <option value="all">All</option>
                        <option value="au">Australia</option>
                        <option value="br">Brazil</option>
                        <option value="ca">Canada</option>
                        <option value="fr">France</option>
                        <option value="de">Germany</option>
                        <option value="mx">Mexico</option>
                        <option value="nz">New Zealand</option>
                        <option value="it">Italy</option>
                        <option value="za">South Africa</option>
                        <option value="es">Spain</option>
                        <option value="pt" selected>Portugal</option>
                        <option value="us">U.S.A.</option>
                        <option value="uk">United Kingdom</option>
                    </select>
                </div>
            </div>


            <div class="col-sm-2" id="filter-column">
                <div id="locationField">
                  <input class="search" id="form-input" name="student_city" placeholder="Enter a city" type="text" />
                </div>
            </div>

, ( - URL) , .

, name, :

var queryString = $('#myFormId').serialize();

queryString , : country=pt&student_city=Montijo%2C+Portugal

, . JSON? Angular? , , ?

, , , . , ? , , , , , .

+4
1

, . $stateParams ui-router, . https://github.com/angular-ui/ui-router/wiki/URL-Routing.

, A, . . , , :

{
    color: red,
    sort: ascending,
    sortBy: name
}

AJAX $http, :

$http.get("http(s)://serverurl.com/api/endpoint?color=red&sort=ascending&sortBy=name").then(function(resp){
      //handle response as you want
      //you can go to next state via $state.go("stateB", {params: resp.data}) and render view according to the new data
});

B , $stateParams , , A.

, , , :

:

{ 
  name: 'people', 
  url: '/people',
  resolve: {
    people: function(PeopleService) {
      return PeopleService.getAllPeople();
    }
  }
},

{ 
  name: 'people.person', 
  url: '?isActive&eyeColor',
  resolve: {
    filteredPeople: function(people, $stateParams) {
      var res = [];
      people.find(function(person) { 
        if(shouldWeAddToFiltered($stateParams))
        {
          res.push(person);
        }

      });
      return res;
    }
  },
  controller: function(filteredPeople){
      //do something with filteredPeople array
  }
}

URL . , .

0

Source: https://habr.com/ru/post/1672817/


All Articles