Error while calling a class object from another object in Angular 4 Typescript component












0














Introduction to the problem
Here is my newcomp.component.ts file from Angular 4 project. I created the project using Angular CLI so it is well organized. My app name is cvfy. So the folder and file structure is as follows:
CVFY




-e2e (folder)



-node_modules (folder)



-server (folder)



-src (folder)



--environments (folder)



--assets (folder)



--app (folder)



---app.component.css (file)



---app.component.html (file)



---app.component.spec.ts (file)



---app.component.ts (file)



---app.module.ts (file)



---clientlogos.ts (file)



---imageclass.ts (file)



---clientscomp (folder)



----clientscomp.component.html (file)



----clientscomp.component.spec.ts (file)



----clientscomp.component.ts (file)




I have mentioned the important files and folders above for my problem
I am writing my code in clientscomp.component.ts file and using classes clientlogos.ts and imageclass.ts



clientscomp.component.ts



import { Component, OnInit } from '@angular/core';
import { Clientlogos } from "./../clientlogos";
import { Imageclass } from "./../imageclass";

@Component({
selector: 'app-clientscomp',
templateUrl: './clientscomp.component.html',
styleUrls: ['./clientscomp.component.css']
})

export class ClientscompComponent implements OnInit {

private imageone: Imageclass = [
{"_id":"1", "altClient":"Play Station", "srcClient":"https://cdn3.iconfinder.com/data/icons/flat-icons-web/40/PlayStation-128.png"},
{"_id":"2", "altClient":"Twitter", "srcClient":"https://cdn1.iconfinder.com/data/icons/flat-and-simple-part-1/128/twitter-128.png"}
];

private myrows: Clientlogos = [
{
"_id":"1",
"imagenew": this.imageone
}
];

constructor() { }

ngOnInit() {
}

}


clientlogos.ts



import { Imageclass } from "./imageclass";

export class Clientlogos {
"_id":string;
"imagenew":Imageclass;
}


imageclass.ts



export class Imageclass {
"_id": string;
"altClient": string;
"srcClient": string;
}


The error it shows on running the application is:



Failed to compile.

C:/Users/Dell/Desktop/Learning/cvfy/src/app/clientscomp/clientscomp.component.ts (40,7): Type '{ "_id": string; "imagenew": Imageclass; }' is not assignable to type 'Clientlogos'.
Type '{ "_id": string; "imagenew": Imageclass; }' is not assignable to type 'Clientlogos'.
Object literal may only specify known properties, and '"imagenew"' does not exist in type 'Clientlogos'.









share|improve this question





























    0














    Introduction to the problem
    Here is my newcomp.component.ts file from Angular 4 project. I created the project using Angular CLI so it is well organized. My app name is cvfy. So the folder and file structure is as follows:
    CVFY




    -e2e (folder)



    -node_modules (folder)



    -server (folder)



    -src (folder)



    --environments (folder)



    --assets (folder)



    --app (folder)



    ---app.component.css (file)



    ---app.component.html (file)



    ---app.component.spec.ts (file)



    ---app.component.ts (file)



    ---app.module.ts (file)



    ---clientlogos.ts (file)



    ---imageclass.ts (file)



    ---clientscomp (folder)



    ----clientscomp.component.html (file)



    ----clientscomp.component.spec.ts (file)



    ----clientscomp.component.ts (file)




    I have mentioned the important files and folders above for my problem
    I am writing my code in clientscomp.component.ts file and using classes clientlogos.ts and imageclass.ts



    clientscomp.component.ts



    import { Component, OnInit } from '@angular/core';
    import { Clientlogos } from "./../clientlogos";
    import { Imageclass } from "./../imageclass";

    @Component({
    selector: 'app-clientscomp',
    templateUrl: './clientscomp.component.html',
    styleUrls: ['./clientscomp.component.css']
    })

    export class ClientscompComponent implements OnInit {

    private imageone: Imageclass = [
    {"_id":"1", "altClient":"Play Station", "srcClient":"https://cdn3.iconfinder.com/data/icons/flat-icons-web/40/PlayStation-128.png"},
    {"_id":"2", "altClient":"Twitter", "srcClient":"https://cdn1.iconfinder.com/data/icons/flat-and-simple-part-1/128/twitter-128.png"}
    ];

    private myrows: Clientlogos = [
    {
    "_id":"1",
    "imagenew": this.imageone
    }
    ];

    constructor() { }

    ngOnInit() {
    }

    }


    clientlogos.ts



    import { Imageclass } from "./imageclass";

    export class Clientlogos {
    "_id":string;
    "imagenew":Imageclass;
    }


    imageclass.ts



    export class Imageclass {
    "_id": string;
    "altClient": string;
    "srcClient": string;
    }


    The error it shows on running the application is:



    Failed to compile.

    C:/Users/Dell/Desktop/Learning/cvfy/src/app/clientscomp/clientscomp.component.ts (40,7): Type '{ "_id": string; "imagenew": Imageclass; }' is not assignable to type 'Clientlogos'.
    Type '{ "_id": string; "imagenew": Imageclass; }' is not assignable to type 'Clientlogos'.
    Object literal may only specify known properties, and '"imagenew"' does not exist in type 'Clientlogos'.









    share|improve this question



























      0












      0








      0







      Introduction to the problem
      Here is my newcomp.component.ts file from Angular 4 project. I created the project using Angular CLI so it is well organized. My app name is cvfy. So the folder and file structure is as follows:
      CVFY




      -e2e (folder)



      -node_modules (folder)



      -server (folder)



      -src (folder)



      --environments (folder)



      --assets (folder)



      --app (folder)



      ---app.component.css (file)



      ---app.component.html (file)



      ---app.component.spec.ts (file)



      ---app.component.ts (file)



      ---app.module.ts (file)



      ---clientlogos.ts (file)



      ---imageclass.ts (file)



      ---clientscomp (folder)



      ----clientscomp.component.html (file)



      ----clientscomp.component.spec.ts (file)



      ----clientscomp.component.ts (file)




      I have mentioned the important files and folders above for my problem
      I am writing my code in clientscomp.component.ts file and using classes clientlogos.ts and imageclass.ts



      clientscomp.component.ts



      import { Component, OnInit } from '@angular/core';
      import { Clientlogos } from "./../clientlogos";
      import { Imageclass } from "./../imageclass";

      @Component({
      selector: 'app-clientscomp',
      templateUrl: './clientscomp.component.html',
      styleUrls: ['./clientscomp.component.css']
      })

      export class ClientscompComponent implements OnInit {

      private imageone: Imageclass = [
      {"_id":"1", "altClient":"Play Station", "srcClient":"https://cdn3.iconfinder.com/data/icons/flat-icons-web/40/PlayStation-128.png"},
      {"_id":"2", "altClient":"Twitter", "srcClient":"https://cdn1.iconfinder.com/data/icons/flat-and-simple-part-1/128/twitter-128.png"}
      ];

      private myrows: Clientlogos = [
      {
      "_id":"1",
      "imagenew": this.imageone
      }
      ];

      constructor() { }

      ngOnInit() {
      }

      }


      clientlogos.ts



      import { Imageclass } from "./imageclass";

      export class Clientlogos {
      "_id":string;
      "imagenew":Imageclass;
      }


      imageclass.ts



      export class Imageclass {
      "_id": string;
      "altClient": string;
      "srcClient": string;
      }


      The error it shows on running the application is:



      Failed to compile.

      C:/Users/Dell/Desktop/Learning/cvfy/src/app/clientscomp/clientscomp.component.ts (40,7): Type '{ "_id": string; "imagenew": Imageclass; }' is not assignable to type 'Clientlogos'.
      Type '{ "_id": string; "imagenew": Imageclass; }' is not assignable to type 'Clientlogos'.
      Object literal may only specify known properties, and '"imagenew"' does not exist in type 'Clientlogos'.









      share|improve this question















      Introduction to the problem
      Here is my newcomp.component.ts file from Angular 4 project. I created the project using Angular CLI so it is well organized. My app name is cvfy. So the folder and file structure is as follows:
      CVFY




      -e2e (folder)



      -node_modules (folder)



      -server (folder)



      -src (folder)



      --environments (folder)



      --assets (folder)



      --app (folder)



      ---app.component.css (file)



      ---app.component.html (file)



      ---app.component.spec.ts (file)



      ---app.component.ts (file)



      ---app.module.ts (file)



      ---clientlogos.ts (file)



      ---imageclass.ts (file)



      ---clientscomp (folder)



      ----clientscomp.component.html (file)



      ----clientscomp.component.spec.ts (file)



      ----clientscomp.component.ts (file)




      I have mentioned the important files and folders above for my problem
      I am writing my code in clientscomp.component.ts file and using classes clientlogos.ts and imageclass.ts



      clientscomp.component.ts



      import { Component, OnInit } from '@angular/core';
      import { Clientlogos } from "./../clientlogos";
      import { Imageclass } from "./../imageclass";

      @Component({
      selector: 'app-clientscomp',
      templateUrl: './clientscomp.component.html',
      styleUrls: ['./clientscomp.component.css']
      })

      export class ClientscompComponent implements OnInit {

      private imageone: Imageclass = [
      {"_id":"1", "altClient":"Play Station", "srcClient":"https://cdn3.iconfinder.com/data/icons/flat-icons-web/40/PlayStation-128.png"},
      {"_id":"2", "altClient":"Twitter", "srcClient":"https://cdn1.iconfinder.com/data/icons/flat-and-simple-part-1/128/twitter-128.png"}
      ];

      private myrows: Clientlogos = [
      {
      "_id":"1",
      "imagenew": this.imageone
      }
      ];

      constructor() { }

      ngOnInit() {
      }

      }


      clientlogos.ts



      import { Imageclass } from "./imageclass";

      export class Clientlogos {
      "_id":string;
      "imagenew":Imageclass;
      }


      imageclass.ts



      export class Imageclass {
      "_id": string;
      "altClient": string;
      "srcClient": string;
      }


      The error it shows on running the application is:



      Failed to compile.

      C:/Users/Dell/Desktop/Learning/cvfy/src/app/clientscomp/clientscomp.component.ts (40,7): Type '{ "_id": string; "imagenew": Imageclass; }' is not assignable to type 'Clientlogos'.
      Type '{ "_id": string; "imagenew": Imageclass; }' is not assignable to type 'Clientlogos'.
      Object literal may only specify known properties, and '"imagenew"' does not exist in type 'Clientlogos'.






      angular class object typescript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 5:23









      Cœur

      17.4k9103145




      17.4k9103145










      asked Jun 7 '17 at 12:15









      Vinit KhandelwalVinit Khandelwal

      43




      43
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can use Array in you class, to make it more statically typed.



          class Menu {
          name: string;
          categories: Array<Category>; // initialize it as applicable
          }
          class Category {
          name: string;
          dishes: Array<Dish>;// initialize it as applicable
          }
          class Dish {
          name: string;
          }


          Component



          You can try fill this.menus which will also be strongly typed Array // make sure the code is written to compile.



          getMenus() {
          this.globals.getMenus().subscribe(
          data => {
          this.menus = new Array<Menu>();
          Object.keys(data).forEach(name => {
          //Initialize and push items as per data structure.
          this.menus.push(new Menu(data[name]));
          });

          console.log(this.menus);
          },
          err => { console.log(err) }
          );
          }


          template Arrange the following as a combination of Un-ordered and ordered list.



          <ul>
          <li *ngFor="let menu of menus">
          {{ menu.name }}
          <ul>
          <li *ngFor="let category of menu.categories">
          {{ category.name }}
          <ul>
          <li *ngFor="let dish of category.dishes">
          {{ dish.name }}
          </li>
          </ul>
          </li>
          </ul>
          </li>
          </ul>





          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',
            autoActivateHeartbeat: false,
            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%2f44412359%2ferror-while-calling-a-class-object-from-another-object-in-angular-4-typescript-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You can use Array in you class, to make it more statically typed.



            class Menu {
            name: string;
            categories: Array<Category>; // initialize it as applicable
            }
            class Category {
            name: string;
            dishes: Array<Dish>;// initialize it as applicable
            }
            class Dish {
            name: string;
            }


            Component



            You can try fill this.menus which will also be strongly typed Array // make sure the code is written to compile.



            getMenus() {
            this.globals.getMenus().subscribe(
            data => {
            this.menus = new Array<Menu>();
            Object.keys(data).forEach(name => {
            //Initialize and push items as per data structure.
            this.menus.push(new Menu(data[name]));
            });

            console.log(this.menus);
            },
            err => { console.log(err) }
            );
            }


            template Arrange the following as a combination of Un-ordered and ordered list.



            <ul>
            <li *ngFor="let menu of menus">
            {{ menu.name }}
            <ul>
            <li *ngFor="let category of menu.categories">
            {{ category.name }}
            <ul>
            <li *ngFor="let dish of category.dishes">
            {{ dish.name }}
            </li>
            </ul>
            </li>
            </ul>
            </li>
            </ul>





            share|improve this answer


























              0














              You can use Array in you class, to make it more statically typed.



              class Menu {
              name: string;
              categories: Array<Category>; // initialize it as applicable
              }
              class Category {
              name: string;
              dishes: Array<Dish>;// initialize it as applicable
              }
              class Dish {
              name: string;
              }


              Component



              You can try fill this.menus which will also be strongly typed Array // make sure the code is written to compile.



              getMenus() {
              this.globals.getMenus().subscribe(
              data => {
              this.menus = new Array<Menu>();
              Object.keys(data).forEach(name => {
              //Initialize and push items as per data structure.
              this.menus.push(new Menu(data[name]));
              });

              console.log(this.menus);
              },
              err => { console.log(err) }
              );
              }


              template Arrange the following as a combination of Un-ordered and ordered list.



              <ul>
              <li *ngFor="let menu of menus">
              {{ menu.name }}
              <ul>
              <li *ngFor="let category of menu.categories">
              {{ category.name }}
              <ul>
              <li *ngFor="let dish of category.dishes">
              {{ dish.name }}
              </li>
              </ul>
              </li>
              </ul>
              </li>
              </ul>





              share|improve this answer
























                0












                0








                0






                You can use Array in you class, to make it more statically typed.



                class Menu {
                name: string;
                categories: Array<Category>; // initialize it as applicable
                }
                class Category {
                name: string;
                dishes: Array<Dish>;// initialize it as applicable
                }
                class Dish {
                name: string;
                }


                Component



                You can try fill this.menus which will also be strongly typed Array // make sure the code is written to compile.



                getMenus() {
                this.globals.getMenus().subscribe(
                data => {
                this.menus = new Array<Menu>();
                Object.keys(data).forEach(name => {
                //Initialize and push items as per data structure.
                this.menus.push(new Menu(data[name]));
                });

                console.log(this.menus);
                },
                err => { console.log(err) }
                );
                }


                template Arrange the following as a combination of Un-ordered and ordered list.



                <ul>
                <li *ngFor="let menu of menus">
                {{ menu.name }}
                <ul>
                <li *ngFor="let category of menu.categories">
                {{ category.name }}
                <ul>
                <li *ngFor="let dish of category.dishes">
                {{ dish.name }}
                </li>
                </ul>
                </li>
                </ul>
                </li>
                </ul>





                share|improve this answer












                You can use Array in you class, to make it more statically typed.



                class Menu {
                name: string;
                categories: Array<Category>; // initialize it as applicable
                }
                class Category {
                name: string;
                dishes: Array<Dish>;// initialize it as applicable
                }
                class Dish {
                name: string;
                }


                Component



                You can try fill this.menus which will also be strongly typed Array // make sure the code is written to compile.



                getMenus() {
                this.globals.getMenus().subscribe(
                data => {
                this.menus = new Array<Menu>();
                Object.keys(data).forEach(name => {
                //Initialize and push items as per data structure.
                this.menus.push(new Menu(data[name]));
                });

                console.log(this.menus);
                },
                err => { console.log(err) }
                );
                }


                template Arrange the following as a combination of Un-ordered and ordered list.



                <ul>
                <li *ngFor="let menu of menus">
                {{ menu.name }}
                <ul>
                <li *ngFor="let category of menu.categories">
                {{ category.name }}
                <ul>
                <li *ngFor="let dish of category.dishes">
                {{ dish.name }}
                </li>
                </ul>
                </li>
                </ul>
                </li>
                </ul>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 7 '17 at 16:00









                Vinit KhandelwalVinit Khandelwal

                43




                43






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44412359%2ferror-while-calling-a-class-object-from-another-object-in-angular-4-typescript-c%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

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin