I need to create a directive that acts upon table cells where the table rows are rendered using ng-repeat
-- to that end I have relied in part on this answer to a question entitled "Calling a function when ng-repeat has finished". Unlike that Q&A however, I need to pass in an argument to my directive, and for this I have relied in part on this answer (to a question entitled "Angularjs - Pass argument to directive").
So in my case I've added fixed-column-tooltip
for my directive, and columnselector
as its argument to the <tr>
as follows:
<tr fixed-column-tooltip columnselector=".td-keyField" ng-repeat="trData in trDataWatch">
But when per the second answer, I added what I've learned is an "isolate scope" to my directive, I no longer had access to the original scope necessary as per the first answer:
'use strict';
angular.module('cmt.cases.directives')
.directive('fixedColumnTooltip', function ($timeout) {
return {
restrict: 'A',
scope: {
columnselector: '@'
},
link: function (scope, element, attr) {
if (scope.$last === true) { //undefined because not operating on original scope
...
Is there a way to maintain access to the original scope, but also have access to the columnselector
argument?
Scope in a Directive Well, all of the directives have a scope associated with them. This scope object is used for accessing the variables and functions defined in the AngularJS controllers, and the controller and link functions of the directive.
The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).
restrict is for defining the directive type, and it can be A (Attribute), C (Class), E (Element), and M (coMment) , let's assume that the name of the directive is Doc : Type : Usage.
$scope is a child object that is used to bind the HTML(view) & Javascript(Controller) in a webpage. It is created with the ng-app directive. It is created with the ng-controller directive. It is available globally for all the controllers, i.e, the property assigned with “$rootscope” can be used anywhere.
You could use,
'use strict';
angular.module('cmt.cases.directives')
.directive('fixedColumnTooltip', function ($timeout) {
return {
restrict: 'A',
scope: {
columnselector: '@',
$last: '=$last',
},
link: function (scope, element, attr) {
if (scope.$last === true) {
....
the second parameter to scope will pass $last parameter by reference.
EDIT:
Since the $last is only available in the scope of repeat element, you could get it from the element scope, like this
'use strict';
angular.module('cmt.cases.directives')
.directive('fixedColumnTooltip', function ($timeout) {
return {
srestrict: 'A',
scope: {
columnselector: '@',
},
link: function (scope, element, attrs) {
var elemScope = element.scope();
if (elemScope.$last){
......
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With