1

With reference to the following code snippet:

Plunker Demo 1 which is working fine, I've made the following changes:

HTML

<!DOCTYPE html> <html ng-app="MultiplicationApp"> <head> <link rel="stylesheet" href="multiplication-app.css"> </head> <body ng-init="data='x'"> <div multiplication-table x="5" y="5"> {{ multiplication.value }} </div> <script type="text/javascript" src="//code.angularjs.org/1.3.6/angular.js"></script> <script src="multiplication-app.js"></script> </body> </html> 

multiplication-app.js

var ngModule = angular.module('MultiplicationApp', []) ngModule.directive('multiplicationTable', [function() { return { templateUrl : 'multiplication-table-tpl.html', controllerAs : 'ctrl', transclude: true, bindToController: true, scope: { x : '=', y : '=' }, controller : function() { var x = this.x || 0; var y = this.y || 0; var table = this.rows = []; for(var i=0;i<y;i++) { var array = table[i] = []; for(var j=0;j<x;j++) { array.push(1); } } } } }]); ngModule.directive('multiplicationCell', [function() { return { controllerAs : 'multiplication', bindToController: true, scope: { x : '=', y : '=' }, controller : function() { var x = this.x || 0; //why does this does not resolve the x="$index+1" attribute in the directive. var y = this.y || 0; //why does this does not resolve the y="$parent.$index+1" attribute in the directive. this.value = x * y; // console.log(this); } }; }]); 

multiplication-table.tpl.html

<div ng-repeat="row in ctrl.rows track by $index"> <div ng-repeat="cell in row track by $index"> <div multiplication-cell x="$index+1" y="$parent.$index+1" ng-transclude> </div> </div> </div> 

I am unable to understand why I'm unable to access the multiplication.value from the multiplication controller inside the inner-nested tag.

I've created this plunk to demonstrate this.

Plunker Demo 2

I'm looking for answers to the following:

  1. Why the second Plunk is not working as intended ?
  2. How to make the second plunk working using minimal changes. And this solution should not be identical to the first Plunker.

Note: The multiplicationCell implementation is similar to the multiplicationTable but still not working.

1 Answer 1

1

Basically, Make a template for multiplication that holds the value which we are transcluding.

controllerAs : 'multiplication', templateUrl: 'multiplication.tpl.html', bindToController: true, scope: { x : '=', y : '=' }, controller : function() { var x = this.x || 0; var y = this.y || 0; this.value = x * y; // console.log(this); } }]); 

In order to access multiply value, you need to pass value to its own template so I have created template for child 'multiplication.tpl.html' and what you need is achieve.

multiplication.tpl.html

{{ multiplication.value }} 

Find the Plunker for your answer : http://plnkr.co/edit/glssXrvbVpP2UjDgu3Ed?p=preview

I hope this explanation clear your doubt.

Thanks & cheers!

Sign up to request clarification or add additional context in comments.

9 Comments

Thank You but this doesn't answer my query. I want to know why the second plunk doesn't work. The first one is already working as I stated. I just want to make it working using the controller of the second Plunker. And also wnat to know why the second Plunker is not working just like the first one.
In angular world, we are not passing same value from child/parent. we either pass value from child or we can use a value from parent.
But in multiplicationTable we are accessing the attributes x and y through scope bounded to the controller. Why can't I just use the same approach in multiplicationCell directive?
yes it is working!! Thanks, but its throwing an error to console: angular.js:8525 Error: [ngTransclude:orphan] Illegal use of ngTransclude directive in the template! No parent directive that requires a transclusion found. Element: <div multiplication-cell="" x="$index+1" y="$parent.$index+1" ng-transclude="" class="ng-binding ng-isolate-scope"> http://errors.angularjs.org/1.3.6/ngTransclude/orphan?p0=%3Cdiv%20multiplication-cell%3D%22%22%20x%3D%22%24index%2B1%22%20y%3D%22%24parent.NaNndex%2B1%22%20ng-transclude%3D%22%22%class%3D%22ng-binding%20ng-isolate-scope%22%3E
haven't seen the error earlier. thanks for noticing it and removed error in this plunker : plnkr.co/edit/glssXrvbVpP2UjDgu3Ed?p=preview can you please accept the answer? Thanks
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.