1

I've got next directive:

(function() {
    'use strict';

    angular
        .module('myApp')
        .directive('inner', inner);

    function inner () {

        return {
            restrict: 'A',
            scope: false,
            link: linkFunc
        };

        function linkFunc (scope, element, attrs) {

        }
    }
})();

And HTML:

<span inner>{{vm.number}}</span>

How can I access vm.number's value in linkFunc? I need to take value exactly from content of the span tag.

2

2 Answers 2

2

There are various ways you can do this but here are the 2 most common ways:

ngModel

You could use ng-model like so in your template:

<span inner ng-model="vm.number">{{vm.number}}</span>

In your directive you require the ngModel where you can pull its value:

.directive( 'inner', function(){
    return {
        require: 'ngModel',
        link: function($scope, elem, attrs, ngModel){
            var val = ngModel.$modelValue
        }
    }
})

declare isolate scope properties

<span inner="vm.number">{{vm.number}}</span>

.directive( 'inner', function(){
    return {
        scope: { inner:'=' } ,
        link: function($scope, elem, attrs){
            var val = $scope.inner
        }
    }
})

Some less common ways:

use $parse service to get the value

Using the template again:

<span inner="vm.number">{{vm.number}}</span>

Let's assume you're going to Firstly you'll need to inject the $parse service in your directive's definition. Then inside your link function do the following:

var val = $parse(attrs.inner)

inherited scope for read only

I don't recommend this, because depending on how you defined your directive's scope option, things might get out of sync:

  1. isolate (aka isolated) scopes will not inherit that value and vm.number will probably throw an undefined reference error because vm is undefined in most cases.

  2. inherited scope will inherit the initial value from the parent scope but could diverge during run-time.

  3. no scope will be the only case where it will stay in sync since the directive's $scope reference is the same scope present in the expression {{vm.number}}

Again I stress this is probably not the best option here. I'd only recommend this if you are suffering performance issues from a large number of repeated elements or large number of bindings. More on the directive's scope options - https://spin.atomicobject.com/2015/10/14/angular-directive-scope/

Sign up to request clarification or add additional context in comments.

1 Comment

I was interested in the opportunity to take exactle content, without attributes (to avoid copies in code). But as you mentioned, it is impossible. I would prefer 'declare isolate scope properties', because it allows to pass function's result. Thanks!
0

Well, In Angular directive, Link function can do almost everything controller can.

To make it very simple, we use one of them most of the time.

var app = angular.module('app', []);

    app.controller('AppCtrl', function ($scope) {

          $scope.number = 5;

    }).directive('inner', function () {

      return {
        restrict: 'A',
        scope: false,
        link: function (scope, element, attrs) {
          var number = scope.number;
          console.log(number);
        }
      }

    });

Inside html :

<div inner ng-model="number">{{number}}</div>

https://plnkr.co/edit/YbXYpNtu7S3wc0zuBw3u?p=preview

In order to take value from HTML, Angular provides ng-model directive which is works on two way data binding concepts.

There are other ways which is already explain by @jusopi :)

cheers!

1 Comment

I wanna take value from controller to directive, not vice versa.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.