A list of tricky angular 1 concepts, and the best explanations I found.
What's the differences between ngModel formatter and parsers? source
Formatters change how model values will appear in the view.
Parsers change how view values will be saved in the model.
//format text going to user (model to view)
ngModel.$formatters.push(function(value) {
return value.toUpperCase();
});
//format text from the user (view to model)
ngModel.$parsers.push(function(value) {
return value.toLowerCase();
});
The require:'ngModel' and require:'^ngModel' allow you to inject the model attached to the element or its parent element on which the directive is bound to.
Its basically an easiest way to pass ngModel into the link/compile function instead passing it using a scope option. Once you have access to ngModel, you can change its value using $setViewValue, make it dirty/clean using $formatters, apply watchers, etc.
controllerAs: "us" – Assigns the controller object to $scope.us. If you’re used to putting variables directly on scope, this will be new to you.
bindToController: true – Binds scope variables to the controller. In combination with controllerAs, this lets us access the user variable as us.user within the view.
Should business logic go into controller or link function in custom direcgives? source
In short, the link function is where DOM manipulation belongs, a good amount of your business logic for a specific instance of something would be in your controller function.
var linker = function(scope, element, attrs){
element.on('click', scope.someFunctionDefinedInTheControllerFunction);
};
Why $scope.obj.data is better than $scope.data source
What's the differences between ngModel formatter and parsers? source
What does require: 'ngModel' do? source
require:'ngModel'
andrequire:'^ngModel'
allow you to inject the model attached to the element or its parent element on which the directive is bound to.$setViewValue
, make it dirty/clean using $formatters, apply watchers, etc.What does ng-transclude do? source
What does controllerAs and bindToController do?
controllerAs
: "us" – Assigns the controller object to $scope.us. If you’re used to putting variables directly on scope, this will be new to you.bindToController: true
– Binds scope variables to the controller. In combination with controllerAs, this lets us access the user variable as us.user within the view.Should business logic go into controller or link function in custom direcgives? source
Why $scope.obj.data is better than $scope.data source