Watch angular attrs and scope

attrs.$observe и $scope.$parent.$watch
Подробнее:

Взято из реализации директивы пагинации

replace: true,
scope: {}, // to prevent parent scope data to be injected into directive's scope
link: function ($scope, $el, attrs)
{
	var params = {
			total: null,
			perPage: null,
			offset: null,
			maxPages: 5
		};

	// start rendering only when all parameters are ready
	for (var k in params)
	{
		if (params.hasOwnProperty(k))
		{
			(function () {
				var key = k;
				attrs.$observe(key, function (value)
				{
					if (! value) {
						return;
					}
					// rerender on interpolation
					$scope.$parent.$watch(value, function (value) {
						params[key] = value;
						if (params.total && params.perPage && params.maxPages && params.offset) {
							render();
						}
					});
				});
			}());
		}
	}
...

LEAVE A COMMENT