Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access parent scope, if needed to?

I'm struggling with this. Rating is a directive from Angular Bootstrap which I don't control, I can define a controller, and I'm trying to serialize the rating through it, however I don't seem to be able to have access to the parent article. I'm new to AngularJS so this is probably the silliest question, but I was experimenting with $scope.$parent, which in does gives me access to an article property, but when I do $scope.$parent.article it returns undefined, there must be another way.

This is the markup in question:

<div class="article full" data-ng-model="article">
  <header class="page-header">
    <h4>{{article.title}}</h4>
    <div class="article-rating" rating ng-controller="RatingsCtrl" value="article.rating" max="5"></div>
  </header>
  <!-- ... -->
</div>

This is the directive in question https://github.com/angular-ui/bootstrap/blob/master/src/rating/rating.js, and this is my attempt on getting something to work, which doesn't add too much context, but as requested:

angular.module("myApp")
  .controller("RatingsCtrl", ($scope) ->
    console.log $scope.$parent.article #=> undefined

    $scope.$watch 'value', (newVal, oldVal) ->
      console.log "newVal = #{newVal} oldVal=#{oldVal}"
      console.log $scope.$parent.article #=> undefined
  )
like image 493
blackxored Avatar asked Sep 19 '13 09:09

blackxored


1 Answers

I'd need to see the script to reply more precisely, but you said that $scope.parent has an article property, but $scope.$parent.article is undefined.

If that's not a typo, you should use $scope.parent.article instead...

like image 194
redShadow Avatar answered Sep 23 '22 04:09

redShadow