Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to use AngularJS to build my application, and I'm faced to a problem with the $scope context.

This is my app main file :

var app = angular.module('app', 
[ 
    'matchCtrl', 
    'matchService',
    'ngRoute'
]);

app.config(function($routeProvider){
    $routeProvider
        .when('/', { templateUrl: '../app/views/partials/home.html' })
        // all routes for matches
        .when('/matches', { templateUrl: '../app/views/partials/matches.html', controller: "matchController" })
        .when('/matches/:id', { templateUrl: '../app/views/partials/match_place.html', controller: "matchController" })

        .otherwise({ redirectTo: '/' });
});

I've got one controller that's call matchController. It look like this and have just to catch the data with the get and show method of my match factory.

Here, the controller :

angular.module('matchCtrl', [])

    .controller('matchController', function($scope, $http, Match) {

        // object to hold all the data for the new match form
        $scope.matchData = {};

        // loading variable to show the spinning loading icon
        $scope.loading = true;

        // get all the matches first and bind it to the $scope.matches object
        Match.get()
            .success(function(data) {
                $scope.matches = data;
                $scope.loading = false;
            });

        $scope.showPlaces = function(id) {

            $scope.loading = true;

            console.log($scope);

            Match.show(id)
                .success(function(data){
                    $scope.places = data;
                    console.log($scope.places);
                    $scope.loading = false;
                });
        };
    });

Here, the factory. I'm using Laravel as an API Backend to catch data, and all routes "api/*" are defined in my Laravale routes file.

angular.module('matchService', [])

    .factory('Match', function($http) {

        return {
            get : function() {
                return $http.get('api/matches');
            },
            show : function(id) {
                return $http.get('api/matches/' + id);
            },
        }

    });

Here my index file, and my partial where I want to use the $scope.place variable.

<!doctype html>
    <html lang="en" ng-app="app">
    <head>
        <meta charset="UTF-8">
        <title>Laravel and Angular test</title>

        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js"></script>
        <script src="https://code.angularjs.org/1.2.16/angular-route.min.js"></script>

    </head>
    <body class="container">
        <div ng-view></div>

        <script src="js/controllers/matchCtrl.js"></script>
        <script src="js/controllers/placeCtrl.js"></script>
        <script src="js/services/matchService.js"></script>
        <script src="js/services/placeService.js"></script>
        <script src="js/app.js"></script>
    </body>
</html>

The partial view :

<div class="place text-center">
    <ul>
        <li>{{ place.name }}</li>
    </ul>
</div>

My problem is that on the partial view, the $scope is not the same that in my main view. And then, I can't access to my $scope.place variable and use it into the view.

share|improve this question
    
I dont see any ng-controller directive in your main view that is creating any scope... –  Sten Muchow May 13 at 10:16
    
Oh, I see. I'm just stupid. I just give the ng-controller="matchController" attribute in my main view and it's work. And then, I can put others controllers in my route for any partial view ? –  Lilrom May 13 at 10:22
    
exactly then you can add controllers to take over scope when u change routes... not to be advertising ui-router when you arent looking for it, but i really recommend this solution to the routing paradigm in angular... –  Sten Muchow May 13 at 10:26
add comment

2 Answers

You can also add ng-controller at

<div class="place text-center" ng-controller="MatchController">
    <ul>
        <li>{{ place.name }}</li>
    </ul>
</div>
share|improve this answer
add comment

Add a controller to you index to get access to its scope

<!doctype html>
    <html lang="en" ng-app="app">
    <head>
        <meta charset="UTF-8">
        <title>Laravel and Angular test</title>

        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js"></script>
        <script src="https://code.angularjs.org/1.2.16/angular-route.min.js"></script>

    </head>
    <body class="container" ng-controller="MatchController">
        <div ng-view></div>

        <script src="js/controllers/matchCtrl.js"></script>
        <script src="js/controllers/placeCtrl.js"></script>
        <script src="js/services/matchService.js"></script>
        <script src="js/services/placeService.js"></script>
        <script src="js/app.js"></script>
    </body>
</html>
share|improve this answer
    
I did it, and it's work. Thank you for your answer! –  Lilrom May 13 at 10:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.