Suppose I've got two separate components
. One defines the my-app
element, one defines the child
element.
I want the child
component to be a dependency of the my-app
(root) component.
How is this done?
myapp.component.js
(function(app) {
app.AppComponent = ng.core
.Component({
selector: 'my-app',
template: '<h1>My First Angular 2 App!!</h1>'
})
.Class({
constructor: function() {}
});
})(window.app || (window.app = {}));
child.component.js
(function(app) {
app.ChildComponent = ng.core
.Component({
selector: 'child',
template: '<h3>This is the child</h3>',
})
.Class({
constructor: function() {}
});
})(window.app || (window.app = {}));
In fact, it's something similar to what we have in the Typescript version ;-). You need to configure providers:
bootstrap
methodproviders
propertyThere are two great blog posts that can help you when writing Angular2 applications with ES5:
Here is a complete working sample with ES5 only:
index.html for JavaScript files to include
Definition of the service
Definition of the component
Bootstrap the
Cmp
component as application componentDependency injection at application level. Simply add a second parameter to the
bootstrap
function. Its value corresponds to an array containing theService
object.Dependency injection at component level. Simply add a
providers
property in the component configuration object. Its value is an array containing theService
object.If you want to use a component inside another one, simply leverage the
directives
attribute of the view, as described below. TheCmpChild
component is used within theCmp
one.Hope it helps you, Thierry