0

Referring to the code given below, I would like to be able to load viewTeam URL into ng-view from the showTeam() function. How can I do this?

<html>

<head>
    <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
    <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular-route.min.js"></script>

    <script>
        var teamApp = angular.module("teamApp", ['ngRoute']);

        teamApp.controller('teamController', function($scope, $http) {

            $http
                    .get('/teams')
                    .success(function(response) {
                        $scope.teams = response;
                    }
                    );


            var showTeam = function(id) {

            }
        });

        teamApp.config(['$routeProvider', function($routeProvider) {
            $routeProvider.

            when('/addTeam', {
                templateUrl: 'addTeam.htm',
                controller: 'AddTeamController'
            }).

            when('/viewTeam', {
                templateUrl: 'viewTeam.htm',
                controller: 'ViewTeamController'
            }).

            otherwise({
                redirectTo: '/addTeam'
            });
        }]);

        teamApp.controller('AddTeamController', function($scope) {

        });

        teamApp.controller('ViewTeamController', function($scope, $routeParams) {

        });

    </script>
</head>

<body>

<div ng-app = "teamApp" ng-controller="teamController">

    <button ng-click="newTeam()">new</button>

    <div ng-repeat="team in teams" >
        Name: {{team.name}}
        <br />
        Description: {{team.description}}
        <br />
        <button ng-click="showTeam(team.id)">show</button>
    </div>

    <div ng-view></div>

    <script type = "text/ng-template" id = "addTeam.htm">
        <h2> Add Team </h2>
        To be implemented later.
    </script>

    <script type = "text/ng-template" id = "viewTeam.htm">
        Name: {{team.name}}
        Description: {{team.description}}
    </script>
</div>
</body>
</html>

3 Answers 3

1

You need to change your $routeprovider for viewTeam to expect an id parameter. Then get that id in the viewTeamController using routeparams. Here is how you do it. Just follow the pattern in the script below:

<script>
            var teamApp = angular.module("teamApp", ['ngRoute']);

            teamApp.controller('teamController', function($scope, $http,$location) {

                $http
                        .get('/teams')
                        .success(function(response) {
                            $scope.teams = response;
                        }
                        );


                var showTeam = function(id) {
               $location.url("#/viewTeam/" + id);//there are other means as well.
                }
            });

            teamApp.config(['$routeProvider', function($routeProvider) {
                $routeProvider.

                when('/addTeam', {
                    templateUrl: 'addTeam.htm',
                    controller: 'AddTeamController'
                }).

                when('/viewTeam/:id', {
                    templateUrl: 'viewTeam.htm',
                    controller: 'ViewTeamController'
                }).

                otherwise({
                    redirectTo: '/addTeam'
                });
            }]);

            teamApp.controller('AddTeamController', function($scope) {

            });

            teamApp.controller('ViewTeamController', function($scope, $routeParams) {
            alert($routeParams.id);//you get the route params here.
            });
    </script>

When navigating from view:

 <a href="#viewTeam/45">  

In your case:

<a href="#viewTeam/{{team.id}}"> //to navigate from view.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Alex. Your answer led me to the solution.
I am glad i could be of help to you :)
1

In your teamController do this -

var showTeam = function(id) {
    $location.path('/viewTeam.htm').search({'id': id});
}

In ViewTeamController you can get id like this

//Get id from url params
$location.search('id');

Comments

0

you can use $location to change the path

    $scope.showTeam = function(view){
        $location.path("/viewTeam"); // path not hash
    }

Comments

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.