I have a question regarding a specific part in an example from Thinkster. I believe my confusion is due to my limited understanding of JavaScript and AngularJS fundamentals. I've been teaching myself since December, starting with the basics of JavaScript and now diving into Angular. If you could explain it to me as if I were 5 years old, I would greatly appreciate it! Check out the Thinkster Page here
App.js
app.controller("ChoreCtrl", function($scope){
$scope.logChore = function(chore){
alert(chore + " is done!");
};
});
app.directive("kid", function() {
return {
restrict: "E",
scope: {
done: "&"
},
template: '<input type="text" ng-model="chore">' +
//I'm slightly confused about this particular section
'{{chore}}' +
'<div class="button" ng-click="done({chore: chore})">I\'m done</div>'
};
});
HTML
<div ng-app="choreApp">
<div ng-controller="ChoreCtrl">
<kid done="logChore(chore)"></kid>
</div>
</div>
How does {chore:chore} actually work? According to Thinkster:
The {chore:chore} syntax links the chore value from the input model to be passed to the logChore function when we use 'done="logChore(chore)"' (in the kid directive)
Here are my thoughts:
- Clicking invokes "done", which further triggers "logChore(chore)" based on the HTML attribute
- I assume that
"{chore:chore}"
in App.js is being passed to logChore, so it's like calling logChore(chore:chore)?
Why can't we simply use ng-click=(done(chore))
? What is exactly happening with {chore:chore}
? It might be obvious that I am pretty lost in all this haha.
Thank you very much for your help!