One possible solution:
return {
restrict: 'E',
require: 'ngModel',
scope: {
model: '=ngModel',
},
link: function(scope, element, attributes, ngModel) {
if (!ngModel) {
return;
}
console.log(scope.model); // data passed to directive
}
}
Then,
<custom-directive ngModel="data"></custom-directive>
Now, your $scope.data
will be accessible as scope.model
inside the directive. Keep in mind that any changes made to scope.model
within the directive will also affect $scope.data
.
To prevent this, you can simply modify the ngModel
:
return {
restrict: 'E',
scope: {
data: '=myData',
},
link: function(scope, element, attributes) {
console.log(scope.data); // data passed to directive
}
}
And then,
<custom-directive my-data="data"></custom-directive>