Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs directive - get element bound text content

Tags:

angularjs

How do you get the value of the binding based on an angular js directive restrict: 'A'?

<span directiverestrict> {{binding}} </span>

I tried using elem[0].innerText but it returns the exact binding '{{binding}}' not the value of the binding

.directive('directiverestrict',function() {
    return {
        restrict:'A',
        link: function(scope, elem, attr) {
            // I want to get the value of the binding enclosed in the elements directive without ngModels
            console.log(elem[0].textContent) //----> returns '{{binding}}'
        }
    };
});
like image 789
Michael Rosales Avatar asked May 06 '15 05:05

Michael Rosales


People also ask

Which of the directive is used to display text in AngularJS?

ng-app: The ng-app Directive in AngularJS is used to define the root element of an AngularJS application. This directive automatically initializes the AngularJS application on page load. It can be used to load various modules in AngularJS Application.

What is ngInit?

The ngInit directive allows you to evaluate an expression in the current scope. This directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit : aliasing special properties of ngRepeat , as seen in the demo below.

Which directive tells AngularJS what to do when an HTML element is clicked?

The ng-click directive tells AngularJS what to do when an HTML element is clicked.

Which directive do we use to inform AngularJS about the parts controlled by it?

The ngRef attribute tells AngularJS to assign the controller of a component (or a directive) to the given property in the current scope. It is also possible to add the jqlite-wrapped DOM element to the scope. The ngRepeat directive instantiates a template once per item from a collection.


3 Answers

You can use the $interpolate service, eg

.directive('logContent', function($log, $interpolate) {
  return {
    restrict: 'A',
    link: function postLink(scope, element) {
      $log.debug($interpolate(element.text())(scope));
    }
  };
});

Plunker

like image 53
Phil Avatar answered Nov 10 '22 18:11

Phil


 <span directiverestrict bind-value="binding"> {{binding}} </span>

SCRIPT

directive("directiverestrict", function () {
   return {
           restrict : "A",
           scope : {
                      value : '=bindValue'
                   },
           link : function (scope,ele,attr) {
                alert(scope.value); 
              }
      }
});
like image 41
Arepalli Praveenkumar Avatar answered Nov 10 '22 19:11

Arepalli Praveenkumar


During the link phase the inner bindings are not evaluated, the easiest hack here would be to use $timeout service to delay evaluation of inner content to next digest cycle, such as

$timeout(function() {
   console.log(elem[0].textContent);
},0);
like image 25
Chandermani Avatar answered Nov 10 '22 19:11

Chandermani