mmoscosa
mmoscosa

Reputation: 397

Trouble accessing Parent controller scope (AngularJS)

am trying to go over AngularJS as a complete n00b, installed everything using Yeoman, grunt and bower.

I have been fiddling around with the code nicely but now am stuck at trying to access parent scope. See Code below

'use strict';

angular.module('castLibraryApp')
  .controller('library', function ($scope) {
    $scope.movies =  [
        {title: 'Sherlock Holmes', poster: 'http://bit.ly/19p8cla'},
        {title: 'Star Trek', poster: 'http://bit.ly/1eAagVa'},
      ];
    $scope.directory  = '~/Movies/';
    console.log($scope.movies);
  })
  .controller('movie', function ($scope, $routeParams) {
    $scope.section  = 'View Movie';
    console.log($scope.movies);
  });

Library controller is called in my main.html template, while the movie controller is accessed in my view.html template. I want to print the information such as name and poster in the view template.

However I keep getting undefined variable

I know it my be a silly error but would really appreciate the help

Upvotes: 0

Views: 1792

Answers (2)

Ankit Mittal
Ankit Mittal

Reputation: 672

In movie controller use

$scope.$parent.movies

and in view.html, use

{{ $parent.movies }}

Upvotes: 2

michael
michael

Reputation: 16341

you may use $scope.$parent to access your library controller. But this is not a good design. Better you put your movies in service: http://docs.angularjs.org/guide/dev_guide.services.understanding_services

Upvotes: 3

Related Questions