Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is a known problem with scoping in directives. You can read the article <a href="https://github.com/angular/angular.js/wiki/The-Nuances-of-Scope-Prototypal-Inheritance" rel="nofollow noreferrer">The Nuances of Scope Prototypal Inheritance</a> to know more about the <code>scoping</code> in angular js.</p> <p>Any primitive value assignment from a child/transcluded scope will create a new instance value instead of changing the parent scopes value</p> <p>In your case you are working with a primitive value <code>selectedNumber</code>.</p> <p>There are two suggested ways to fix it</p> <p>Solution 1 <br /> <em>Use a object instead of primitive value.</em><br /></p> <ol> <li>Change selectedNumber to an object <code>scope.selectedNumber = { num : 1 };</code></li> <li>Change display to <code>&lt;h2&gt;{{ selectedNumber.num }}&lt;/h2&gt;</code></li> <li>Change the click handler in <code>ng-repeat</code> to <code>ng-click="selectedNumber.num = number"</code></li> </ol> <p>Demo: <a href="http://plnkr.co/edit/TxPD9lKFI9T4S5uZ4i07?p=preview" rel="nofollow noreferrer">Plunker</a><br /></p> <p>Solution 2:<br /> <em>Use <code>$parent</code> scope reference<br /></em></p> <ol> <li>Change the click handler in <code>ng-repeat</code> to <code>ng-click="$parent.selectedNumber = number"</code></li> </ol> <p>Demo: <a href="http://plnkr.co/edit/e6ncIbHiKRz6rX94XlXh?p=preview" rel="nofollow noreferrer">Plunker</a></p> <p>Solution 3:<br /> <em>Use a setter in the parent scope<br /></em></p> <ol> <li>Create a setter method in the parent scope like <code>$scope.setSelectedNumber = function(num){ $scope.selectedNumber = num}</code></li> <li>Change the click event to <code>setSelectedNumber(number)</code> (This is the method already used)</li> </ol> <p><em>Update: <br /></em> As suggested by Anders Ekdahl, it is advisable to use the object (solution 1) based solution.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload