1

I have a problem with my a tag - I have a page that present data according to the GET vars.

For example - /foo.php?opt=1 will show table of cities that each one will go to - /foo.php?city=4 that have table of schools that go to /foo.php?school=4 that show table of students etc..

The problem is that the first time it works - when I choose city it will show me the list of schools and change the url, but when I choose school, it changes the URL but I still see the city table, and only if I press F5 it will show me table students.

This is the code:

odinvite.php:

<?php
if (isset($_GET['city']))
{
    include "odbycity.php";
}
else if (isset($_GET['school']))
{
    include "odbyschool.php";
}
else
{
    include "odshowcities.php";
}
?>

odshowcities.php:

<div ng-controller="allcities">

    <button class="btn btn-info" ng-repeat="x in names">
        <a href="/odinvite.php?city={{x.areaid}}">
        {{x.areaname}}</a>
    </button>

</div>

odbyschool.php:

<div ng-controller="odbycity">
    <button class="btn btn-info" ng-repeat="x in names">
        <a href="/odinvite.php?school={{x.schoolid}}">
        {{x.school_name}}</a>
    </button>
</div>

MyAngular.js:

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

myApp.config(function( $locationProvider) {

    $locationProvider.html5Mode(true);
});

myApp.controller ('allcities', function ($scope, $http)
{
    $http.get("fetch_json_sql.php?option=1")
        .then(function (response)
        {
            $scope.names = response.data.result;
        });
    console.log($scope.names);
});

myApp.controller ('odbycity', function ($scope, $http, $location)
{
    $scope.cityid=$location.search().city;
    console.log($scope.cityid);
    $http.get("fetch_json_sql.php?option=2&cityid="+$scope.cityid)
        .then(function (response)
        {
            $scope.names = response.data.result;
        });

});

myApp.controller ('odbyschool', function ($scope, $http ,$location)
{
    $scope.schoolid = $location.search().school;
    console.log($scope.schoolid);
    $http.get("fetch_json_sql.php?option=4&schoolid="+$scope.schoolid)
        .then(function (response)
        {
            $scope.names = response.data.result;
        });

});

What can be the problem?

I tried to make 100% change of the URL - <a href="www.google.com">link</a> and it didn't work. just changed the URL without redirect.

Thanks!

5
  • Please add your route configuration. Commented Apr 2, 2017 at 8:35
  • @lin just add it..Thats ok? Commented Apr 2, 2017 at 8:38
  • Well m8, AngularJS is for SPA. Its not that easy to combine your routes with an backend directly. You should use ngRoute or uiRouter without having your templates rendered by a backend. I still see no route configuration for /odinvite.php?school or odinvite.php?city. Your question is "why are my routes not working". Commented Apr 2, 2017 at 8:42
  • This is all the code I have...I'm just started to learn AngularJS. What do I need to know about this routes? Commented Apr 2, 2017 at 8:44
  • Is that my solution...? - docs.angularjs.org/api/ng/directive/ngHref Commented Apr 2, 2017 at 8:46

1 Answer 1

1

You should stop rendering your templates with a backend. AngularJS is for SPA. If you need data provided by a backend try to implement an API e.g. a RESTful API. you need to configure your routes for example like in this runnable demo plnkr. It uses ui-router. Please note, this is just a demo. You should be able to put your logic into that prototype. I prepared all routes you need by using some dummy data. Just include your existing API in the controllers and you should be fine.

var myApp = angular.module("myApp", ['ui.router']);

myApp.config(function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.when("", "/main");

    $stateProvider
        .state("main", {
            url: "/main",
            templateUrl: "main.html"
        })
        .state("main.listSchools", {
            url: "/listSchools/:schoolId",
            templateUrl: "schools.html"
        }) 
        .state("main.listAreas", {
            url: "/listAreas/:areaId",
            templateUrl: "areas.html"
        });
});


myApp.controller('mainMenuController', function ($scope) {
    $scope.schools = [{
      schoolid: 1,
      name: 'Test School 1'
    },{
      schoolid: 5,
      name: 'Test School 5'
    },{
      schoolid: 11,
      name: 'Test School 11'
    }];
    $scope.areas = [{
      areaid: 3,
      name: 'Test area 3'
    },{
      areaid: 7,
      name: 'Test area 7'
    },{
      areaid: 19,
      name: 'Test area 7'
    }];
});



myApp.controller('listSchoolController', function ($scope, $state) {
  $scope.schoolId = $state.params.schoolId;
});


myApp.controller('listAreaController', function ($scope, $state) {
  $scope.areaId = $state.params.areaId;
});
Sign up to request clarification or add additional context in comments.

8 Comments

There is no option to do that without the Route? And BTW - I started to read about "app.config(function($routeProvider) { $routeProvider .when("/", { templateUrl" " - Is that the same?
You could fake the routing but this is not what AngularJS was made for. ngRoute is a different route provider. I recommend to use uiRouter because its much more flexible and give you a lot of more options to design your routes.
Ok.. I will need to do some homework to understand what you wrote - altough it looks kind of logical. do you have some recommended site that explain this?
Hope for some YouTube but thats fine too :) Thanks a lot!!
BTW - When I using Route - there is a chance I'll have to config the server? Because I using a free host server not a dedicated one..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.