Rudi Ørnhøj
Rudi Ørnhøj

Reputation: 221

Angular component expression binding without template

I'm trying to output my scope data from my component, but having a hard time figuring out how to do it without a local template.

For different reasons I need to have the markup ind the HTML file and not being parsed in with the js load

This is the dummy code so far: (codepen: http://codepen.io/anon/pen/qNBBRN)

HTML:

<comp>
  {{ $ctrl.testing }}
</comp>

Non-working JS code:

angular
      .module('Test', [])
      .component('comp', {
        controller: myCtrl,
      });

function myCtrl() {
  var model = this;
  model.testing = '123';
}

document.addEventListener('DOMContentLoaded', function() {
  angular.bootstrap(document, ['Test']);
});

And this is what I want to avoid even though it works:

angular
  .module('Test', [])
  .component('comp', {
    controller: myCtrl,
    template: '{{ $ctrl.testing }}',
  });

function myCtrl() {
  var model = this;
  model.testing = '123';
}

document.addEventListener('DOMContentLoaded', function() {
  angular.bootstrap(document, ['Test']);
});

Upvotes: 4

Views: 3880

Answers (1)

Daniel Higueras
Daniel Higueras

Reputation: 2404

A solution to what you need is using bindings to relate the component's inner private scope with the parent scope.

JS

angular
  .module('Test', [])
  .component('comp', {
    controller: myCtrl,
    bindings: {
      testing: '='
    }
  });

function myCtrl() {
   var model = this;
   model.testing = '123';
}

HTML

<comp testing="$ctrl.testing">
  {{ $ctrl.testing }}
</comp>

Plunkr example: http://plnkr.co/edit/jLeDyBTFA9OU7oqK5HSI?p=preview

Upvotes: 1

Related Questions