Why can't I define my variable with bracket notation in Angular 2

Optiq

I've been trying to define a variable with bracket notation just to get familiar with how it works and aren't getting any results. Unfortunatelt I'm on on a tablet with a keyboard using Plunkr so I can't open the console and see what's going wrong, hopefully someone can spot the problem without needing an error message from the console.

my code inside the component class currently looks like this

var  demoA:string='name';
var  demoB:string='city';
var  demoD:string='';
var  demoC:{[propName: string]:string}= {};

demoC['demoA']= demoD;

seing that I'm using typescript I didn't originally have var at the beginning of the variables but I eventually decided to set it up as it was on the MDN site and still didn't get any results, the widget just stays stck on loading. I also tried adding this. to everything in the attempt to create the new property. My goal is to have demoC with a property of name='' inside of it. What am I missing here?

Here's my entire file on plunker

//our root app component
import {Component, NgModule, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
    </div>
    <p>{{demoA}}</p>
    <p>{{demoB}}</p>
    <br />
    <p>{{demoC}}</p>
  `,
})
export class App {
  name:string;

  var  demoA:string='name';
  var  demoB:string='city';
  var  demoD:string='';
  var  demoC:{[propName: string]:string}= {};

  demoC[demoA]= demoD;

  constructor() {
    this.name = `Angular! v${VERSION.full}`
  }
}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}
Suren Srapyan

You have used the string with name demoA, not the variables value. It will create a property with name demoA. Remove the '' from the [] notation to let the [] syntax to evaluate the expression and get the value from it.

demoC[demoA]= demoD;

This is the example in TypeScript playground

According to your Plunker code, you have some errors

Remove the var parts from the variables declarations. Put the assignment operator of the demoC into your constructor. It understands your statement as a duplicate variable.

import {Component, NgModule, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
    </div>
    <p>{{demoA}}</p>
    <p>{{demoB}}</p>
    <br />
    <p>{{demoC}}</p>
  `,
})
export class App {
  name: string;

  demoA: string = 'name';
  demoB: string = 'city';
  demoD: string = '';
  demoC: { [propName: string]: string} = {};

  constructor() {
    this.name = `Angular! v${VERSION.full}`;
    this.demoC[this.demoA]= this.demoD;
  }
}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}

Code example

Plunker code example

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Can I force dot/bracket notation?

分類Dev

Why can't I access my objects member variable?

分類Dev

Why can't Angular2 inject my service?

分類Dev

I can't change the value of my variable

分類Dev

Why can't I access my private variable inside a function in PHP?

分類Dev

Why can't I access this global variable?

分類Dev

Why can't I define a Geometry with a Transform in XAML?

分類Dev

How can I define a variable with another variable?

分類Dev

Why can't I find my express code at AWS EB'S EC2 instance?

分類Dev

Why can't I select cells in my WPF datagrid?

分類Dev

Why can't I use my column alias in WHERE clause?

分類Dev

Why can't I extend my desktop 12.04 in KDE?

分類Dev

Why can't I crash my system with a fork bomb?

分類Dev

Why can't I see remote video in my WebRTC app?

分類Dev

Why can't I open my txt file in Ubuntu?

分類Dev

Why can't I download my package through packagist?

分類Dev

Why can't I name a sigilless variable v+digit?

分類Dev

Why can't I log things in Angular guard?

分類Dev

Can't assign value to variable using subscribe() method in Angular 2

分類Dev

Why can't I assign to a variable a JSON value that I can see in Debug mode?

分類Dev

I can't write object literal notation in Dev Console?

分類Dev

Why is my javascript function not being invoked for simple MVC tutorial? Why can't I debug either?

分類Dev

Spread operator with bracket notation

分類Dev

Why won't my macro variable resolve?

分類Dev

Why isn't my JavaScript variable working?

分類Dev

Why doesn't my subclass' variable exist?

分類Dev

Why Make doesn't recognize my variable?

分類Dev

Why can I pass an integer as a task parameter but can't pass a struct variable?

分類Dev

Why isn't my html code outputting the variable's value when I use Flask to render a template?

Related 関連記事

  1. 1

    Can I force dot/bracket notation?

  2. 2

    Why can't I access my objects member variable?

  3. 3

    Why can't Angular2 inject my service?

  4. 4

    I can't change the value of my variable

  5. 5

    Why can't I access my private variable inside a function in PHP?

  6. 6

    Why can't I access this global variable?

  7. 7

    Why can't I define a Geometry with a Transform in XAML?

  8. 8

    How can I define a variable with another variable?

  9. 9

    Why can't I find my express code at AWS EB'S EC2 instance?

  10. 10

    Why can't I select cells in my WPF datagrid?

  11. 11

    Why can't I use my column alias in WHERE clause?

  12. 12

    Why can't I extend my desktop 12.04 in KDE?

  13. 13

    Why can't I crash my system with a fork bomb?

  14. 14

    Why can't I see remote video in my WebRTC app?

  15. 15

    Why can't I open my txt file in Ubuntu?

  16. 16

    Why can't I download my package through packagist?

  17. 17

    Why can't I name a sigilless variable v+digit?

  18. 18

    Why can't I log things in Angular guard?

  19. 19

    Can't assign value to variable using subscribe() method in Angular 2

  20. 20

    Why can't I assign to a variable a JSON value that I can see in Debug mode?

  21. 21

    I can't write object literal notation in Dev Console?

  22. 22

    Why is my javascript function not being invoked for simple MVC tutorial? Why can't I debug either?

  23. 23

    Spread operator with bracket notation

  24. 24

    Why won't my macro variable resolve?

  25. 25

    Why isn't my JavaScript variable working?

  26. 26

    Why doesn't my subclass' variable exist?

  27. 27

    Why Make doesn't recognize my variable?

  28. 28

    Why can I pass an integer as a task parameter but can't pass a struct variable?

  29. 29

    Why isn't my html code outputting the variable's value when I use Flask to render a template?

ホットタグ

アーカイブ