2

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):

var app = angular
        .module("Demo", ["ngRoute"])
        .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"
                })

        })

        .controller("studentDetailsController", function ($scope, $http, $routeParams) {
            $http({
                url: "http://enrolin.in/test/login.php?p=fetchone&&id=",
                method: "get",
                params: { id: $routeParams.id }
            }).then(function (response) {
                $scope.stud = response.data;

            })
        })
.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;
                                    })
         })
        .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.

2 Answers 2

1

I think you can use window.location.href instead of tag to redirect. Or you can use "hash state" to control your router in func

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

2 Comments

Hi FlowerChng, Thanks for replying. But I am very new to angular, can you, please tell me how to write that code?
Hi @akhilesh-khajuria, the native javascript window.location.href="YOUR URL" will work. Btw, I prefer to use angular-ui-router, it 's a better way to config and control your router.
1

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.

4 Comments

Hello, Oshadha. I just tried your fix, but it's not working. And studentDetails part is not problematic, its the studentSearch part I think.
Hi @AkhilEshKhajuria, Please try replacing $location.url("/studentsSearch/" + $scope.name); with $location.path("/studentsSearch/" + $scope.name);
I think I'm not getting the question. Could you please elaborate it more to me? Try what what I suggested in my previous comment solution with following: when("/studentSearch/:name?", { templateUrl: "templates/studentSearch.html", controller: "studentSearchController" })
Oshadha, I just asked the question again here : stackoverflow.com/questions/37565899/… see if it helps

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.