Angular doesn't display property value











up vote
1
down vote

favorite
1












I've trying to do a basic Angular display of ParentObject->Row->Cell. So a ParentObject has an array of Rows and each row has an array of Cells.



My parent object looks like this:



export class ParentObject implements OnInit {
private gameRows: GamerowComponent;

constructor() {
this.gameRows = ;

for (var i: number = 0; i < 11; i++) {
this.gameRows[i] = new GamerowComponent();
}

this.gameRows[1].gameCells[0].text = 'A';
this.gameRows[2].gameCells[0].text = 'B';
this.gameRows[3].gameCells[0].text = 'C';
this.gameRows[4].gameCells[0].text = 'D';
this.gameRows[5].gameCells[0].text = 'E';
this.gameRows[6].gameCells[0].text = 'F';
this.gameRows[7].gameCells[0].text = 'G';
this.gameRows[8].gameCells[0].text = 'H';
this.gameRows[9].gameCells[0].text = 'I';
this.gameRows[10].gameCells[0].text = 'J';
}


A GameRow just exposes the game cells via properties:



export class GamerowComponent implements OnInit {
private _gameCells: GamecellComponent;

constructor() {
this._gameCells = ;
for (var i:number=0; i < 11; i++) {
this._gameCells[i] = new GamecellComponent();
}
}

ngOnInit() {
}

get gameCells(): GamecellComponent{
return this._gameCells;
}

set gameCells(value: GamecellComponent) {
this._gameCells = value;
}
}


And the cell is just a text and cssclass object:



export class GamecellComponent implements OnInit {
private _text: string;
private _cssClass: string;

constructor() {
this._cssClass = 'tablemarker';
}

ngOnInit() {
}

get text(): string {
return this._text;
}

set text(value: string) {
this._text = value;
}
get cssClass(): string {
return this._cssClass;
}

set cssClass(value: string) {
this._cssClass = value;
}
}


I set up the html for a table / row / cell view:



ParentObject:



<table class="no-spacing">
<tr *ngFor="let gameRow of gameRows">
<app-gamerow></app-gamerow>
</tr>
</table>


Gamerow:



<app-gamecell *ngFor="let cell of gameCells"></app-gamecell>


Gamecell:



<td class="{{cssClass}}">{{text}}</td>


I correctly get an HTML table rendered with 11 rows and cells. The cssClass is correctly rendered, but the text never shows.



When I break the script in the browser after the instantiation of the 11 rows, they all have their text set correctly. How come the staticly set cssClass text works (set in the class constructor) but the assignment from a parent down through the children does not?










share|improve this question






















  • Is app-gamecell the GamecellComponent?
    – user184994
    Nov 8 at 16:45










  • Yes, it is the gamecellcomponent
    – Josh
    Nov 8 at 16:46










  • Best way to achieve that is to use @Input and basic Angular component interaction, otherwise you're "breaking" Angular component communication in that way.
    – Dyd666
    Nov 8 at 16:49















up vote
1
down vote

favorite
1












I've trying to do a basic Angular display of ParentObject->Row->Cell. So a ParentObject has an array of Rows and each row has an array of Cells.



My parent object looks like this:



export class ParentObject implements OnInit {
private gameRows: GamerowComponent;

constructor() {
this.gameRows = ;

for (var i: number = 0; i < 11; i++) {
this.gameRows[i] = new GamerowComponent();
}

this.gameRows[1].gameCells[0].text = 'A';
this.gameRows[2].gameCells[0].text = 'B';
this.gameRows[3].gameCells[0].text = 'C';
this.gameRows[4].gameCells[0].text = 'D';
this.gameRows[5].gameCells[0].text = 'E';
this.gameRows[6].gameCells[0].text = 'F';
this.gameRows[7].gameCells[0].text = 'G';
this.gameRows[8].gameCells[0].text = 'H';
this.gameRows[9].gameCells[0].text = 'I';
this.gameRows[10].gameCells[0].text = 'J';
}


A GameRow just exposes the game cells via properties:



export class GamerowComponent implements OnInit {
private _gameCells: GamecellComponent;

constructor() {
this._gameCells = ;
for (var i:number=0; i < 11; i++) {
this._gameCells[i] = new GamecellComponent();
}
}

ngOnInit() {
}

get gameCells(): GamecellComponent{
return this._gameCells;
}

set gameCells(value: GamecellComponent) {
this._gameCells = value;
}
}


And the cell is just a text and cssclass object:



export class GamecellComponent implements OnInit {
private _text: string;
private _cssClass: string;

constructor() {
this._cssClass = 'tablemarker';
}

ngOnInit() {
}

get text(): string {
return this._text;
}

set text(value: string) {
this._text = value;
}
get cssClass(): string {
return this._cssClass;
}

set cssClass(value: string) {
this._cssClass = value;
}
}


I set up the html for a table / row / cell view:



ParentObject:



<table class="no-spacing">
<tr *ngFor="let gameRow of gameRows">
<app-gamerow></app-gamerow>
</tr>
</table>


Gamerow:



<app-gamecell *ngFor="let cell of gameCells"></app-gamecell>


Gamecell:



<td class="{{cssClass}}">{{text}}</td>


I correctly get an HTML table rendered with 11 rows and cells. The cssClass is correctly rendered, but the text never shows.



When I break the script in the browser after the instantiation of the 11 rows, they all have their text set correctly. How come the staticly set cssClass text works (set in the class constructor) but the assignment from a parent down through the children does not?










share|improve this question






















  • Is app-gamecell the GamecellComponent?
    – user184994
    Nov 8 at 16:45










  • Yes, it is the gamecellcomponent
    – Josh
    Nov 8 at 16:46










  • Best way to achieve that is to use @Input and basic Angular component interaction, otherwise you're "breaking" Angular component communication in that way.
    – Dyd666
    Nov 8 at 16:49













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I've trying to do a basic Angular display of ParentObject->Row->Cell. So a ParentObject has an array of Rows and each row has an array of Cells.



My parent object looks like this:



export class ParentObject implements OnInit {
private gameRows: GamerowComponent;

constructor() {
this.gameRows = ;

for (var i: number = 0; i < 11; i++) {
this.gameRows[i] = new GamerowComponent();
}

this.gameRows[1].gameCells[0].text = 'A';
this.gameRows[2].gameCells[0].text = 'B';
this.gameRows[3].gameCells[0].text = 'C';
this.gameRows[4].gameCells[0].text = 'D';
this.gameRows[5].gameCells[0].text = 'E';
this.gameRows[6].gameCells[0].text = 'F';
this.gameRows[7].gameCells[0].text = 'G';
this.gameRows[8].gameCells[0].text = 'H';
this.gameRows[9].gameCells[0].text = 'I';
this.gameRows[10].gameCells[0].text = 'J';
}


A GameRow just exposes the game cells via properties:



export class GamerowComponent implements OnInit {
private _gameCells: GamecellComponent;

constructor() {
this._gameCells = ;
for (var i:number=0; i < 11; i++) {
this._gameCells[i] = new GamecellComponent();
}
}

ngOnInit() {
}

get gameCells(): GamecellComponent{
return this._gameCells;
}

set gameCells(value: GamecellComponent) {
this._gameCells = value;
}
}


And the cell is just a text and cssclass object:



export class GamecellComponent implements OnInit {
private _text: string;
private _cssClass: string;

constructor() {
this._cssClass = 'tablemarker';
}

ngOnInit() {
}

get text(): string {
return this._text;
}

set text(value: string) {
this._text = value;
}
get cssClass(): string {
return this._cssClass;
}

set cssClass(value: string) {
this._cssClass = value;
}
}


I set up the html for a table / row / cell view:



ParentObject:



<table class="no-spacing">
<tr *ngFor="let gameRow of gameRows">
<app-gamerow></app-gamerow>
</tr>
</table>


Gamerow:



<app-gamecell *ngFor="let cell of gameCells"></app-gamecell>


Gamecell:



<td class="{{cssClass}}">{{text}}</td>


I correctly get an HTML table rendered with 11 rows and cells. The cssClass is correctly rendered, but the text never shows.



When I break the script in the browser after the instantiation of the 11 rows, they all have their text set correctly. How come the staticly set cssClass text works (set in the class constructor) but the assignment from a parent down through the children does not?










share|improve this question













I've trying to do a basic Angular display of ParentObject->Row->Cell. So a ParentObject has an array of Rows and each row has an array of Cells.



My parent object looks like this:



export class ParentObject implements OnInit {
private gameRows: GamerowComponent;

constructor() {
this.gameRows = ;

for (var i: number = 0; i < 11; i++) {
this.gameRows[i] = new GamerowComponent();
}

this.gameRows[1].gameCells[0].text = 'A';
this.gameRows[2].gameCells[0].text = 'B';
this.gameRows[3].gameCells[0].text = 'C';
this.gameRows[4].gameCells[0].text = 'D';
this.gameRows[5].gameCells[0].text = 'E';
this.gameRows[6].gameCells[0].text = 'F';
this.gameRows[7].gameCells[0].text = 'G';
this.gameRows[8].gameCells[0].text = 'H';
this.gameRows[9].gameCells[0].text = 'I';
this.gameRows[10].gameCells[0].text = 'J';
}


A GameRow just exposes the game cells via properties:



export class GamerowComponent implements OnInit {
private _gameCells: GamecellComponent;

constructor() {
this._gameCells = ;
for (var i:number=0; i < 11; i++) {
this._gameCells[i] = new GamecellComponent();
}
}

ngOnInit() {
}

get gameCells(): GamecellComponent{
return this._gameCells;
}

set gameCells(value: GamecellComponent) {
this._gameCells = value;
}
}


And the cell is just a text and cssclass object:



export class GamecellComponent implements OnInit {
private _text: string;
private _cssClass: string;

constructor() {
this._cssClass = 'tablemarker';
}

ngOnInit() {
}

get text(): string {
return this._text;
}

set text(value: string) {
this._text = value;
}
get cssClass(): string {
return this._cssClass;
}

set cssClass(value: string) {
this._cssClass = value;
}
}


I set up the html for a table / row / cell view:



ParentObject:



<table class="no-spacing">
<tr *ngFor="let gameRow of gameRows">
<app-gamerow></app-gamerow>
</tr>
</table>


Gamerow:



<app-gamecell *ngFor="let cell of gameCells"></app-gamecell>


Gamecell:



<td class="{{cssClass}}">{{text}}</td>


I correctly get an HTML table rendered with 11 rows and cells. The cssClass is correctly rendered, but the text never shows.



When I break the script in the browser after the instantiation of the 11 rows, they all have their text set correctly. How come the staticly set cssClass text works (set in the class constructor) but the assignment from a parent down through the children does not?







angular typescript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 16:42









Josh

7,099104690




7,099104690












  • Is app-gamecell the GamecellComponent?
    – user184994
    Nov 8 at 16:45










  • Yes, it is the gamecellcomponent
    – Josh
    Nov 8 at 16:46










  • Best way to achieve that is to use @Input and basic Angular component interaction, otherwise you're "breaking" Angular component communication in that way.
    – Dyd666
    Nov 8 at 16:49


















  • Is app-gamecell the GamecellComponent?
    – user184994
    Nov 8 at 16:45










  • Yes, it is the gamecellcomponent
    – Josh
    Nov 8 at 16:46










  • Best way to achieve that is to use @Input and basic Angular component interaction, otherwise you're "breaking" Angular component communication in that way.
    – Dyd666
    Nov 8 at 16:49
















Is app-gamecell the GamecellComponent?
– user184994
Nov 8 at 16:45




Is app-gamecell the GamecellComponent?
– user184994
Nov 8 at 16:45












Yes, it is the gamecellcomponent
– Josh
Nov 8 at 16:46




Yes, it is the gamecellcomponent
– Josh
Nov 8 at 16:46












Best way to achieve that is to use @Input and basic Angular component interaction, otherwise you're "breaking" Angular component communication in that way.
– Dyd666
Nov 8 at 16:49




Best way to achieve that is to use @Input and basic Angular component interaction, otherwise you're "breaking" Angular component communication in that way.
– Dyd666
Nov 8 at 16:49












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










You should pass the values to child through @Input. Below is the modified version of your code.




Parent Component




<table class="no-spacing">
<tr *ngFor="let gameRow of gameRows">
<app-gamerow [row]="gameRow"></app-gamerow>
</tr>
</table>



Gamerow Component




ts



export class GamerowComponent implements OnInit {

@Input() row;

ngOnInit() {
}
}


html



<app-gamecell *ngFor="let cell of row.gameCells" [cell]="cell"></app-gamecell>



Gamecell Component




ts



export class GamecellComponent implements OnInit {

@Input() cell;
private _cssClass: string;

constructor() {
this._cssClass = 'tablemarker';
}

ngOnInit() {
}

get cssClass(): string {
return this._cssClass;
}

set cssClass(value: string) {
this._cssClass = value;
}
}


html



<td class="{{cssClass}}">{{cell.text}}</td>





share|improve this answer




























    up vote
    3
    down vote













    You shouldn't be instantiating the components directly-- leave that to Angular.



    Instead, hold your data in normal objects, and pass them into the components.



    In GameCellComponent, you can add an input, like so:



    @Input()
    public text: String;

    @Input()
    public cssClass: String;


    You can then pass those values in, like so:



    <div *ngFor="let cell of gameCells">
    <app-gamecell [text]="cell.text" [cssClass]="cell.cssClass"></app-gamecell>
    </div>





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212310%2fangular-doesnt-display-property-value%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      You should pass the values to child through @Input. Below is the modified version of your code.




      Parent Component




      <table class="no-spacing">
      <tr *ngFor="let gameRow of gameRows">
      <app-gamerow [row]="gameRow"></app-gamerow>
      </tr>
      </table>



      Gamerow Component




      ts



      export class GamerowComponent implements OnInit {

      @Input() row;

      ngOnInit() {
      }
      }


      html



      <app-gamecell *ngFor="let cell of row.gameCells" [cell]="cell"></app-gamecell>



      Gamecell Component




      ts



      export class GamecellComponent implements OnInit {

      @Input() cell;
      private _cssClass: string;

      constructor() {
      this._cssClass = 'tablemarker';
      }

      ngOnInit() {
      }

      get cssClass(): string {
      return this._cssClass;
      }

      set cssClass(value: string) {
      this._cssClass = value;
      }
      }


      html



      <td class="{{cssClass}}">{{cell.text}}</td>





      share|improve this answer

























        up vote
        0
        down vote



        accepted










        You should pass the values to child through @Input. Below is the modified version of your code.




        Parent Component




        <table class="no-spacing">
        <tr *ngFor="let gameRow of gameRows">
        <app-gamerow [row]="gameRow"></app-gamerow>
        </tr>
        </table>



        Gamerow Component




        ts



        export class GamerowComponent implements OnInit {

        @Input() row;

        ngOnInit() {
        }
        }


        html



        <app-gamecell *ngFor="let cell of row.gameCells" [cell]="cell"></app-gamecell>



        Gamecell Component




        ts



        export class GamecellComponent implements OnInit {

        @Input() cell;
        private _cssClass: string;

        constructor() {
        this._cssClass = 'tablemarker';
        }

        ngOnInit() {
        }

        get cssClass(): string {
        return this._cssClass;
        }

        set cssClass(value: string) {
        this._cssClass = value;
        }
        }


        html



        <td class="{{cssClass}}">{{cell.text}}</td>





        share|improve this answer























          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You should pass the values to child through @Input. Below is the modified version of your code.




          Parent Component




          <table class="no-spacing">
          <tr *ngFor="let gameRow of gameRows">
          <app-gamerow [row]="gameRow"></app-gamerow>
          </tr>
          </table>



          Gamerow Component




          ts



          export class GamerowComponent implements OnInit {

          @Input() row;

          ngOnInit() {
          }
          }


          html



          <app-gamecell *ngFor="let cell of row.gameCells" [cell]="cell"></app-gamecell>



          Gamecell Component




          ts



          export class GamecellComponent implements OnInit {

          @Input() cell;
          private _cssClass: string;

          constructor() {
          this._cssClass = 'tablemarker';
          }

          ngOnInit() {
          }

          get cssClass(): string {
          return this._cssClass;
          }

          set cssClass(value: string) {
          this._cssClass = value;
          }
          }


          html



          <td class="{{cssClass}}">{{cell.text}}</td>





          share|improve this answer












          You should pass the values to child through @Input. Below is the modified version of your code.




          Parent Component




          <table class="no-spacing">
          <tr *ngFor="let gameRow of gameRows">
          <app-gamerow [row]="gameRow"></app-gamerow>
          </tr>
          </table>



          Gamerow Component




          ts



          export class GamerowComponent implements OnInit {

          @Input() row;

          ngOnInit() {
          }
          }


          html



          <app-gamecell *ngFor="let cell of row.gameCells" [cell]="cell"></app-gamecell>



          Gamecell Component




          ts



          export class GamecellComponent implements OnInit {

          @Input() cell;
          private _cssClass: string;

          constructor() {
          this._cssClass = 'tablemarker';
          }

          ngOnInit() {
          }

          get cssClass(): string {
          return this._cssClass;
          }

          set cssClass(value: string) {
          this._cssClass = value;
          }
          }


          html



          <td class="{{cssClass}}">{{cell.text}}</td>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 17:00









          Sunil Singh

          5,4571625




          5,4571625
























              up vote
              3
              down vote













              You shouldn't be instantiating the components directly-- leave that to Angular.



              Instead, hold your data in normal objects, and pass them into the components.



              In GameCellComponent, you can add an input, like so:



              @Input()
              public text: String;

              @Input()
              public cssClass: String;


              You can then pass those values in, like so:



              <div *ngFor="let cell of gameCells">
              <app-gamecell [text]="cell.text" [cssClass]="cell.cssClass"></app-gamecell>
              </div>





              share|improve this answer

























                up vote
                3
                down vote













                You shouldn't be instantiating the components directly-- leave that to Angular.



                Instead, hold your data in normal objects, and pass them into the components.



                In GameCellComponent, you can add an input, like so:



                @Input()
                public text: String;

                @Input()
                public cssClass: String;


                You can then pass those values in, like so:



                <div *ngFor="let cell of gameCells">
                <app-gamecell [text]="cell.text" [cssClass]="cell.cssClass"></app-gamecell>
                </div>





                share|improve this answer























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  You shouldn't be instantiating the components directly-- leave that to Angular.



                  Instead, hold your data in normal objects, and pass them into the components.



                  In GameCellComponent, you can add an input, like so:



                  @Input()
                  public text: String;

                  @Input()
                  public cssClass: String;


                  You can then pass those values in, like so:



                  <div *ngFor="let cell of gameCells">
                  <app-gamecell [text]="cell.text" [cssClass]="cell.cssClass"></app-gamecell>
                  </div>





                  share|improve this answer












                  You shouldn't be instantiating the components directly-- leave that to Angular.



                  Instead, hold your data in normal objects, and pass them into the components.



                  In GameCellComponent, you can add an input, like so:



                  @Input()
                  public text: String;

                  @Input()
                  public cssClass: String;


                  You can then pass those values in, like so:



                  <div *ngFor="let cell of gameCells">
                  <app-gamecell [text]="cell.text" [cssClass]="cell.cssClass"></app-gamecell>
                  </div>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 at 16:50









                  user184994

                  10.7k11526




                  10.7k11526






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212310%2fangular-doesnt-display-property-value%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Schultheiß

                      Liste der Kulturdenkmale in Wilsdruff

                      Android Play Services Check