Reputation: 119
<grid-sort col="lMember_id" variable="lMember_id"></grid-sort>
directive
app.directive("gridSort", function () {
return {
template:
'<button ng-click="Direction=-1;SetSortValue();orderBy();" ng-init="info.CurrentPage=0"><i class="fa-angle-double-up fa"></i></button>' +
'<button ng-click="Direction=1; SetSortValue();orderBy();" ng-init="info.CurrentPage=1000"><i class="fa-angle-double-down fa"></i></button>'
,
link: function (scope, element, attrs) {
scope.SetSortValue = function () {
if (scope.Direction == -1) {
scope.info.CurrentPage = 0;
scope.info.Direction = 'Asc';
}
else if (scope.Direction == 1) {
scope.info.Direction = 'Desc';
scope.info.CurrentPage = 1000;
}
}
}
};
});
my controller on ng-click
$scope.orderBy = function () {
var data = $scope.info;
Request.GetTabledata("SelectMemberData", data, $scope, true);
};
I want to bind col attribute value with scope.info.colname.. When I declare scope then orderBy() method not working. when i am not declare scope all method working but i don't able to get col value
Upvotes: 0
Views: 48
Reputation: 85578
Update: Ah, now I see what you are trying to achieve! Your approach could work if you only are using the directive once - but since you are using it multiple times you end up overriding both your directive attributes and your $scope
variables.
You can solve this by using a child scope, i.e: Instead of mingling with the parent scope, hold your variables and methods internally or "local" to each instance of the directive.
Here is a refactored version of the directive using a child scope which only updates the child scopes' $parent
info
, that is your main $scope
:
.directive("gridSort", function() {
return {
template:
'<button ng-click="Direction=-1;SetSortValue();orderBy();" ng-init="info.CurrentPage=0"><i class="fa-angle-double-up fa"></i>xx</button>' +
'<button ng-click="Direction=1; SetSortValue();orderBy();" ng-init="info.CurrentPage=1000"><i class="fa-angle-double-down fa"></i>xx</button>'
,
scope: {
col: '@',
variable: '@',
},
controller: function($scope) {
$scope.info = {
colName: $scope.col
}
$scope.SetSortValue = function() {
if ($scope.Direction == -1) {
$scope.info.CurrentPage = 0;
$scope.info.Direction = 'Asc';
} else if ($scope.Direction == 1) {
$scope.info.Direction = 'Desc';
$scope.info.CurrentPage = 1000;
}
$scope.$parent.info = $scope.info;
}
},
link: function(scope, element, attrs) {
//any additional processing
}
};
});
scope: {}
literal to the directivecontroller: function($scope {}
$scope
through $scope.$parent
demo -> http://next.plnkr.co/edit/uxiHC9jgebgJc3DW
Upvotes: 1