E
E
Egor2014-09-23 00:04:14
JavaScript
Egor, 2014-09-23 00:04:14

Angularjs: How to make a directive that is called after variable substitution?

Welcome all.
There is 1 controller, 1 directive and html.
The controller has $scope.variable.
There is such html

<div class="description" my-directive>
{{variable}}
</div>

The task is as follows:
The directive must count the number of characters in $scope.variable and output this number.
I'm trying to get in the directive like this:
link: function(scope,element){
var text = element.text();
console.log(text);  // в text лежит {{variable}} а нужен сам текст этой переменной
}

Found a variant using $timeout with 0 time. But somehow I don't like it and it doesn't seem reliable. Can someone tell me how to be?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2014-09-23
Protko @Fesor

angular.module('app')
.directive('myDirective', function () {
    return function (scope, el, attr) {
        scope.$watch('variable', function (text) {
              console.log('$scope.variable = %s', text);
        }
    }
});

and it is even better to use isolated scopes. If you just need to display the number of characters, it will be easier to do this with filters.

D
Dron Krzh, 2014-09-23
@ walkman7

Well, use scope.variable

S
Sergey Romanov, 2014-09-24
@Serhioromano

And in my opinion, you don’t need to betray or insert {{variable}} inside the directive at all.
jsfiddle.net/asrus2v7

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question