How would you react on model changes to trigger some further action?

Technology CommunityCategory: AngularJSHow would you react on model changes to trigger some further action?
VietMX Staff asked 3 years ago
Problem

For instance, say you have an input text field called email and you want to trigger or execute some code as soon as a user starts to type in their email.

This can be achieved by using $watch function in the controller.

function MyCtrl($scope) {
	$scope.email = '';

	$scope.$watch('email', function (newValue, oldValue) {
		if ($scope.email.length > 0) {
			console.log('User has started writing into email');
		}
	});
}