Reputation: 365
AngularJS newb here, with a lot of questions.
For example, in this directive snippet:
link: function(scope, element, attrs) {
scope.hideModal = function () {
console.log("hide modal fxn called");
scope.show = false;
};
},
template:'<div class=\"ng-modal\" ng-show=\"$scope.show\">' +
'<div class=\"ngdialog-overlay\" ng-click=\"hideModal()\"></div>' +
'<div class=\"ng-modal-dialog\" ng-style=\"dialogStyle\">' +
'<div class=\"ng-modal-close\" ng-click=\"hideModal()\">X</div>' +
'<div class=\"ng-modal-dialog-content\" ng-transclude></div>' +
'<div style=\"left:80%\"><button type = \"button\" style=\"float:right\">Next</button></div>' +
'</div>' +
'</div>'
I'm asking these because I can't get the hideModal() function to work no matter where I put it (either directive or in controller), and I want to understand why.
I'd appreciate it very much if you can answer me in layman's terms as much as possible. AngularJS is being such a pain to understand for me and I'm not that well-versed in it yet, so there's a 100% chance that you'll get blank stares from me if you do otherwise.
Thank you.
Upvotes: 0
Views: 784
Reputation: 16368
$scope
. The scope
in the link
function is exactly the same as the $scope
you would $inject
in your controller.link
function. I only use link
if I need DOM manipulation, i.e. through jQuery$scope
.link
function entirely.the general rule is, do you need DOM
manipulation? Use the link
function otherwise just stick with controller
.
The difference is in execution time, controller
function is executed before compilation while link
is executed after the template compilation.
That's why you would use the link function for DOM
manipulation. During the controller
phase the template is not yet compiled, thus you can't use DOM
manipulation inside the constructor of your function.
And if you don't need DOM
manipulation at all, it might be better to look at the new .component
syntax. This is much easier to understand compared to the .directive
syntax. Keep in mind the .component
syntax is just limited abstraction of .directive
Upvotes: 2