I've read some similar answers to my question, such as this one though can't translate it to my own requirements - comes from a lack of understanding...
I have a controller:
appControllers.controller('TransactionsCtrl', ['$scope', 'InfiniteScrollingDataService', function ($scope, dataService) {
// setup our scope properties
$scope.$root.title = 'Transactions';
var urlQuery = {
skip: 0,
take: 25,
search: '',
order: 'DateTimeCreated',
orderBy: 'desc',
transactionTypeID: null,
transactionStatusID: null
};
var apiUrl = 'api/transactions';
$scope.transactions = new dataService(apiUrl, urlQuery);
$scope.Filter = function (senderParent, type, id) {
$scope.FilterApplied = true;
console.log('filter in controller: ' + senderParent + ' ' + type + ' ' + id);
}
}]);
And I have a directive:
appDirectives.directive('bootstrapListItems', ['$rootScope', function ($rootScope) {
return {
restrict: 'A',
templateUrl: 'bootstrapDropDownItems.html',
link: function (scope, element, attrs) {
scope.type = attrs.useGlobaljsonObject;
scope.parentElement = attrs.id;
scope.items = [];
var obj = $rootScope.globalJSON[scope.type];
for (o in obj) {
scope.items.push({ key: o, value: obj[o] })
}
}
}
}]);
And the template for my directive:
<script type="text/ng-template" id="bootstrapDropDownItems.html">
<li class="active"><a href="#" class="lnkFilterList">- Any -</a></li>
<li ng-repeat="item in items">
<a href="#" class="lnkFilterList" ng-click="Filter(parentElement, type, item.key)">{{item.value}}</a>
</li>
</script>
If I don't isolate the scope of the directive then the controller is called correctly, and i see the console logging out my arguments.
However, I (think) I need to isolate the scope as there will be multiples of this directive on the page.
when I add scope: {}
to my directive the controller function is no longer called.
I also tried changing my ng-click
to ng-click="$parent.Filter(.....)"
- that didnt' seem to work either.
Can anybody please point me in the right direction?