0

I am learning Angular, so here is my testapp : http://enrolin.in/test/#/students

Now here I want to search the database by name. So I created the php that returns exactly what I need. Here is the php : http://enrolin.in/test/login.php?p=fetchbyname&&name=ak You have to replace name in the url to anything you need to search. I also created a partial page that returns absolutely correct results, here is the page: http://enrolin.in/test/#/studentSearch/ak Everything was fine till now But here is the problem:

When I try to search in http://enrolin.in/test/#/students , angularJS does not route me to something like http://enrolin.in/test/#/studentSearch/ak but instead to the default that I have set in $routeProvider

Here is my angularJS (I have removed some unimportant code):

The route provider:

.config(function ($routeProvider) {
        $routeProvider


            .when("/students/:id", {
                templateUrl: "templates/studentDetails.html",
                controller: "studentDetailsController"
            })
            .when("/studentSearch/:name", {
                templateUrl: "templates/studentSearch.html",
                controller: "studentSearchController"
            })
            .otherwise({
                redirectTo: "/home"
            })

    })

The Controller that passes the link:

.controller("studentsController", function ($scope, $http, $route,$location) {
        $scope.searchStudent=function(){
            if($scope.name){
                $location.url("/studentsSearch/" + $scope.name);
            }
            else{
                $location.url("/studentsSearch/");
            }
        }

        $scope.reloadData=function(){
            $route.reload();
        }
         $http.get("http://enrolin.in/test/login.php?p=fetchall")
                                .then(function (response) {
                                    $scope.students = response.data;
                                })
     })

The controller that fetches data and displays:

.controller("studentSearchController", function ($scope, $http, $routeParams) {
        if($routeParams.name)
        {
        $http({
            url: "http://enrolin.in/test/login.php?p=fetchbyname&&name=",
            method: "get",
            params: { name: $routeParams.name }
        }).then(function (response) {
            $scope.studs = response.data;

        })
    }
    else
    {
        $http.get("http://enrolin.in/test/login.php?p=fetchall")
                                .then(function (response) {
                                    $scope.students = response.data;
                                })
    }
    })

Previously everytime I wanted to put a link in html to route I used to write like <a href="#/courses">courses</a> But now when I want to put it in the function instead, I am not sure what to write. Please Help.

1 Answer 1

1

Hi @AkhilEshKhajuria,

You are not using the same name what you have mentioned in the routing config. Routing name is "/studentSearch/:name?" but you have used in the function as "/studentsSearch/".

Please try replacing $location.url("/studentsSearch/" + $scope.name); with $location.path("/studentsSearch/" + $scope.name);

Correct the naming issue and it should work.

I tried this and it works fine.

Sign up to request clarification or add additional context in comments.

3 Comments

Did you check for javascript errors using browser developer tools? Anyway can you just tell me the step that follow when you are trying this? On which page you try this?
Hi @AkhilEshKhajuria, Found the issue. Your function is setting the path as $location.path("/studentsSearch/" + $scope.name); and the routing is name is .when("/studentSearch/:name". Names are not matching. Change name and it will work.
Hi @AkhilEshKhajuria, I edit the answer. Check it as well. Solution is tested.

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.