0

I am developing a simple test app in Angular 2 and having problems passing a property to a nested component. See source code below. The template from the nested component renders, but the property passed to the nested component does not render. The rendered output is:

This is a .

Whereas, I would expect the following:

This is a test.

No errors are generated in the browser console. I have also tried utilizing properties: 'inputtext' in the nested component since the name remains the same in the view, but this generates a Can't bind to ... error in the console. As for the Angular2 version, I'm using the latest (Alpha-34).

Any suggestions?

index.html

<html>
  <head>
    <title>Angular 2 Test App</title>
    <script src="https://github.jspm.io/jmcriffey/[email protected]/traceur-runtime.js"></script>
    <script src="https://jspm.io/[email protected]"></script>
    <script src="https://code.angularjs.org/2.0.0-alpha.34/angular2.dev.js"></script>
  </head>
  <body>
    <h1>Welcome to my Angular 2 Test App</h1>
    <app></app>
    <script>System.import('app');</script>
  </body>
</html>

app.ts

import {Component, View, bootstrap} from 'angular2/angular2';

//nested component
@Component({
  selector: 'input-component',
  properties: ['inputtext:inputtext']
})
@View({ template: '<h1>This is a {{inputtext}}.</h1>'})
class inputComponent{}

// root app
@Component({ selector: 'app' })
@View({ 
  directives: [inputComponent], 
  template: '<input-component [inputtext]="test"></input-component>' 
})
class rootApp{}

bootstrap(rootApp);

2 Answers 2

3

It is because you don't bind a variable on your property, you just pass it a literal value. In that case you need to drop the squared brackets and give it like this : inputtext="test". If you use the squared brackets angular2 will expect a variable and will try to map to it, which will give you undefined.

Also note that when you define a property if the variable in your controller has the same name as the property you expose you can use the short form :

properties: ['inputtext']
Sign up to request clarification or add additional context in comments.

1 Comment

still no real documentation on properties. this was a real help!
2
template: '<input-component [inputtext]="\'test\'"></input-component>'

test in your code is an undefined variable. Wrap it with quotes and it should work.

1 Comment

This results in the error: 'Can't bind to 'inputtext' since it isn't a known property of 'input-component'

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.