JavaScript Inject anonymous value into closed directive scope

i was curious if it's possible to inject an value into a directives' close scope. I could't find anything related in the angularjs documentation so I tried something. Does anybody know if there is an more elegant way to solve this?

My approach so far:

app.directive('injectIntoScopeDirective', function(){
  return {
    restrict: 'E',
    scope: {},
    templateUrl: 'inject.tpl.html',
    link: function(scope, elem, attrs){

      angular.forEach(attrs, function(val, attr){
        // variables which should be injected must be prefixed with the inject keyword
        var match = attr.match(/^inject(.*)/);

        if( match !== null && match.length > 1 ){
          scope['injected' + match[1].toLowerCase()] = val
        }
      });
    }
  }
});

Here is a working plunk


A new approach using the bindToController property. Much cleaner.

app.directive('injectIntoScopeDirective', function(){
  return {
    restrict: 'E',
    scope: {},
    controller: function(){

    },
    bindToController: {
      inject: '=' // allows us to bind an object to the directive controller
    },
    controllerAs: 'injectCtrl',
    templateUrl: 'inject.tpl.html'
    // no linking function needed
  }
});

Updated plunk using bindToController property

Answer:1

I'm following the current tutorial and for some reason when ever I click on the Sign in with Google button nothing seems to happen and I'm not entirely sure why. Here is the code: <html> <...

I'm following the current tutorial and for some reason when ever I click on the Sign in with Google button nothing seems to happen and I'm not entirely sure why. Here is the code: <html> <...

I have been working on nvd3 , in d3.js to make the ticks look good their is a nice() provided for ex. var yScale = d3.scale.linear() .domain([0, d3.max(dataset, function(d) { return d[1]; ...

I have been working on nvd3 , in d3.js to make the ticks look good their is a nice() provided for ex. var yScale = d3.scale.linear() .domain([0, d3.max(dataset, function(d) { return d[1]; ...

How can I add an option to resize a bootbox.alert(); ? I'm using Bootbox 4.3.0. bootbox.alert("Success",function(){ }); Here is the link for the bootbox.js library.

How can I add an option to resize a bootbox.alert(); ? I'm using Bootbox 4.3.0. bootbox.alert("Success",function(){ }); Here is the link for the bootbox.js library.

We are currently using Zend Framework 1 with Dojo forms, and are facing a recent problem with some users reporting bugs while using our forms on IE11: for example they are able to check multiple radio ...

We are currently using Zend Framework 1 with Dojo forms, and are facing a recent problem with some users reporting bugs while using our forms on IE11: for example they are able to check multiple radio ...

  1. zend framework latest version
  2. zend framework latest
  3. zend framework latest version download
  4. zend framework latest stable version
  5. zend framework 1 latest version
  6. zend framework 2 latest version
  7. php zend framework latest version