dartwebstormangular-dart

'int' member does not work as @Input() in child component


In the following code, the value of the int member clicked_number does not change whereas the value of the Cint member cint does.

@Component(
  selector: 'my-app',
  template:
  '''
  <h3> Using int:</h3>
  <h4> Clicked Number:{{clicked_number}}</h4>
  <h3> Using Cint:</h3>
  <h4> Clicked Number:{{cint.i}}</h4>

  <div> Numbers: </div><div> <numbers [numbers]="numbers" [clicked_number]="clicked_number" [cint]="cint"></numbers> </div> 
  ''',
  directives: const [materialDirectives, NumbersComponent],
  providers: const [materialProviders],
)
class AppComponent {
  List<int> numbers = [ 1, 2, 3, 4, 5 ];
  int clicked_number = -1;
  Cint cint = new Cint( );
}

Where Cint is simply an int with a class wrapper:

class Cint{ int i;  }

The code for the numbers selector is:

@Component(
  selector: 'numbers',
  styles: const[ '''
  .numbers { float: left; height: 30px; width: 310px; }
  .number:hover { background-color: #F88;}
  .number {cursor: pointer; width: 30px; height: 30px; vertical-align:middle; text-align:center; line-height: 30px; float: left;}
   ''' ],
  template:'''
  <div class="numbers">
   <div *ngFor="let n of numbers" (click)="onClick(n)" class="number"> {{n}} </div>
  </div>
  ''',
  directives: const [CORE_DIRECTIVES,formDirectives],
)

class NumbersComponent {
  @Input()   List<int> numbers;
  @Input()   int clicked_number;
  @Input()   Cint cint;

  void onClick( int i_i ) {
    clicked_number = i_i;
    cint.i = i_i;
  }

While this is not a deal-stopper for me, I would very much like to understand why we can't use int like we can Cint in this example.

I am using Angular-Dart 4.0 in WebStorm 2017.2.3


Solution

  • This behaviour is not an Angular phenomenon, it is a Dart language phenomenon. The following code illustrates the same issue in pure Dart.

    class Cint { int my_int; Cint( this.my_int ); }
      
    void make_2 ( int i_i, Cint i_cinta, Cint i_cintb ) {
      i_i = 2;
      i_cinta.my_int = 2;
      i_cintb = new Cint(2);
    }
    
    void main() {
     int i = 1;
     Cint cinta = new Cint( 1 );
     Cint cintb = new Cint( 1 );
      
      print( "int= $i, Cinta= ${cinta.my_int}, Cintb= ${cintb.my_int} ");
      
      make_2( i, cinta, cintb );
      
      print( "int= $i, Cinta= ${cinta.my_int}, Cintb= ${cintb.my_int} ");
    }
    

    The output is:

    int= 1, Cinta=1, Cintb=1
    int= 1, Cinta=2, Cintb=1
    

    It would be very educational for a Dart language maven to explain what is going on here. If you happen to know such a clever person, please encourage them to provide said explanation.

    Thanks to @KevinMoore for the Cintb example.