A
A
Artem Shchurin2015-06-08 09:27:01
Angular
Artem Shchurin, 2015-06-08 09:27:01

How to make an attribute in your directive that catches changes to variables?

Hello!
I make a directive that will be an attribute, in which the value of the attribute, depending on the variable located in $scope, gave true / false. This is how ng-class works for example. I have just a string in the attribute value and when the variable changes, the value of the expression is not calculated.
Directive:

define(['angular'], function(angular) {
  angular.module('ids.focus', []).directive('focus', function() {
    return {
      link: function($scope, element, attrs){
          console.log($scope.focus);
      },
      scope: {
        focus: '='
      }
    }
  });
});

Its purpose is to give focus.
Usage:
<input type="text" ng-model="item.name" focus="item === editItem"/>

Item - the current element, multiplied with ng-repeat
editItem - a variable in the current $scope, the editable is written to it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valentin Dubrovsky, 2015-06-08
@schurin

define(['angular'], function(angular) {
  angular.module('ids.focus', []).directive('focus', function() {
    return {
      link: function($scope, element, attrs){
          $scope.$watch('focus', function(){
              // тут делаете то что вам нужно при изменение переменной
          });
      },
      scope: {
        focus: '=focus'
      }
    }
  });
});

PS true guys use webpack or browserify

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question