How do I pass a JSON array to an Angular2 template with @Input from a ASP.NET MVC View?










0














I'm new to Angular2 and have many questions.



I have some controllers that I wrote in AngularJS that I now want to convert to Angular2 components.



I would like to pass JSON data to the component and use in the template. In AngularJS I used ng-init to do this.



I have tried initialising the component in the ASP.NET MVC view with:



<review [reviewJson]="@Newtonsoft.Json.JsonConvert.SerializeObject(Model.CustomerReviews)"></review>


The component looks like this:



import Component, OnInit, Input from '@angular/core';

@Component(
selector: 'review',
template: `
<div id="reviews-wrapper" class="row">
<div class="item col-sm-4 col-xs-6" *ngFor="let review of reviewJson">
<div class="review-card">
<div class="review-image"><img [src]="review.Image"/></div>
<div class="review-name">review.Name</div>
<div class="review-text">review.Text</div>
</div>
</div>
</div>
`,
providers:
)

export class ReviewComponent implements OnInit

@Input() reviewJson: any;

constructor()


ngOnInit()
console.log(this.reviewJson);




The app.module file where I declare the review component looks like this:



import NgModule from '@angular/core';
import BrowserModule from '@angular/platform-browser';
import HttpModule from '@angular/http';

import ReviewComponent from './review.component';

@NgModule(
imports: [
BrowserModule,
HttpModule
],
declarations: [
ReviewComponent
],
bootstrap: [ReviewComponent],
providers: [
]
)
export class AppModule


The console.log(this.reviewJson) is showing undefined when I load the page so obviously I'm doing something completely wrong.



Any help appreciated.










share|improve this question























  • Share the ts file where you are using review component.
    – Sunil Singh
    Nov 12 at 15:24















0














I'm new to Angular2 and have many questions.



I have some controllers that I wrote in AngularJS that I now want to convert to Angular2 components.



I would like to pass JSON data to the component and use in the template. In AngularJS I used ng-init to do this.



I have tried initialising the component in the ASP.NET MVC view with:



<review [reviewJson]="@Newtonsoft.Json.JsonConvert.SerializeObject(Model.CustomerReviews)"></review>


The component looks like this:



import Component, OnInit, Input from '@angular/core';

@Component(
selector: 'review',
template: `
<div id="reviews-wrapper" class="row">
<div class="item col-sm-4 col-xs-6" *ngFor="let review of reviewJson">
<div class="review-card">
<div class="review-image"><img [src]="review.Image"/></div>
<div class="review-name">review.Name</div>
<div class="review-text">review.Text</div>
</div>
</div>
</div>
`,
providers:
)

export class ReviewComponent implements OnInit

@Input() reviewJson: any;

constructor()


ngOnInit()
console.log(this.reviewJson);




The app.module file where I declare the review component looks like this:



import NgModule from '@angular/core';
import BrowserModule from '@angular/platform-browser';
import HttpModule from '@angular/http';

import ReviewComponent from './review.component';

@NgModule(
imports: [
BrowserModule,
HttpModule
],
declarations: [
ReviewComponent
],
bootstrap: [ReviewComponent],
providers: [
]
)
export class AppModule


The console.log(this.reviewJson) is showing undefined when I load the page so obviously I'm doing something completely wrong.



Any help appreciated.










share|improve this question























  • Share the ts file where you are using review component.
    – Sunil Singh
    Nov 12 at 15:24













0












0








0







I'm new to Angular2 and have many questions.



I have some controllers that I wrote in AngularJS that I now want to convert to Angular2 components.



I would like to pass JSON data to the component and use in the template. In AngularJS I used ng-init to do this.



I have tried initialising the component in the ASP.NET MVC view with:



<review [reviewJson]="@Newtonsoft.Json.JsonConvert.SerializeObject(Model.CustomerReviews)"></review>


The component looks like this:



import Component, OnInit, Input from '@angular/core';

@Component(
selector: 'review',
template: `
<div id="reviews-wrapper" class="row">
<div class="item col-sm-4 col-xs-6" *ngFor="let review of reviewJson">
<div class="review-card">
<div class="review-image"><img [src]="review.Image"/></div>
<div class="review-name">review.Name</div>
<div class="review-text">review.Text</div>
</div>
</div>
</div>
`,
providers:
)

export class ReviewComponent implements OnInit

@Input() reviewJson: any;

constructor()


ngOnInit()
console.log(this.reviewJson);




The app.module file where I declare the review component looks like this:



import NgModule from '@angular/core';
import BrowserModule from '@angular/platform-browser';
import HttpModule from '@angular/http';

import ReviewComponent from './review.component';

@NgModule(
imports: [
BrowserModule,
HttpModule
],
declarations: [
ReviewComponent
],
bootstrap: [ReviewComponent],
providers: [
]
)
export class AppModule


The console.log(this.reviewJson) is showing undefined when I load the page so obviously I'm doing something completely wrong.



Any help appreciated.










share|improve this question















I'm new to Angular2 and have many questions.



I have some controllers that I wrote in AngularJS that I now want to convert to Angular2 components.



I would like to pass JSON data to the component and use in the template. In AngularJS I used ng-init to do this.



I have tried initialising the component in the ASP.NET MVC view with:



<review [reviewJson]="@Newtonsoft.Json.JsonConvert.SerializeObject(Model.CustomerReviews)"></review>


The component looks like this:



import Component, OnInit, Input from '@angular/core';

@Component(
selector: 'review',
template: `
<div id="reviews-wrapper" class="row">
<div class="item col-sm-4 col-xs-6" *ngFor="let review of reviewJson">
<div class="review-card">
<div class="review-image"><img [src]="review.Image"/></div>
<div class="review-name">review.Name</div>
<div class="review-text">review.Text</div>
</div>
</div>
</div>
`,
providers:
)

export class ReviewComponent implements OnInit

@Input() reviewJson: any;

constructor()


ngOnInit()
console.log(this.reviewJson);




The app.module file where I declare the review component looks like this:



import NgModule from '@angular/core';
import BrowserModule from '@angular/platform-browser';
import HttpModule from '@angular/http';

import ReviewComponent from './review.component';

@NgModule(
imports: [
BrowserModule,
HttpModule
],
declarations: [
ReviewComponent
],
bootstrap: [ReviewComponent],
providers: [
]
)
export class AppModule


The console.log(this.reviewJson) is showing undefined when I load the page so obviously I'm doing something completely wrong.



Any help appreciated.







asp.net-mvc angular angular-components






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 15:34

























asked Nov 12 at 15:18









loz

3817




3817











  • Share the ts file where you are using review component.
    – Sunil Singh
    Nov 12 at 15:24
















  • Share the ts file where you are using review component.
    – Sunil Singh
    Nov 12 at 15:24















Share the ts file where you are using review component.
– Sunil Singh
Nov 12 at 15:24




Share the ts file where you are using review component.
– Sunil Singh
Nov 12 at 15:24












1 Answer
1






active

oldest

votes


















1














You cannot directly bind the MVC models into the angular template - fetch the data as JSON using service and then bind the value to it's @Input decorator



Hope this helps - happy coding :)






share|improve this answer




















  • Thanks, I did think I might be approaching this the wrong way. I liked how I had it setup with building the models in an MVC controller but it sounds like I am going to have to fetch the data in an Angular service.
    – loz
    Nov 12 at 15:28










  • Yep you need you access your controller from the angular service and return the data from the controller as JsonResult - check this link - [angular.io/tutorial/toh-pt4]
    – Rahul
    Nov 12 at 15:33










  • I wrote an Angular2 service to get the JSON data from Web API as suggested and it worked. The most difficult thing to understand was that it was returning an observable which I had to subscribe to so I could put the data in a variable in my component.
    – loz
    Nov 12 at 19:10











  • Great that will be your first learning curve in angular - Observables are awesome check some rxjs libraries that might help you more in understanding - Check this link [angular.io/guide/rx-library]
    – Rahul
    Nov 12 at 19:14











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%2f53265115%2fhow-do-i-pass-a-json-array-to-an-angular2-template-with-input-from-a-asp-net-mv%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









1














You cannot directly bind the MVC models into the angular template - fetch the data as JSON using service and then bind the value to it's @Input decorator



Hope this helps - happy coding :)






share|improve this answer




















  • Thanks, I did think I might be approaching this the wrong way. I liked how I had it setup with building the models in an MVC controller but it sounds like I am going to have to fetch the data in an Angular service.
    – loz
    Nov 12 at 15:28










  • Yep you need you access your controller from the angular service and return the data from the controller as JsonResult - check this link - [angular.io/tutorial/toh-pt4]
    – Rahul
    Nov 12 at 15:33










  • I wrote an Angular2 service to get the JSON data from Web API as suggested and it worked. The most difficult thing to understand was that it was returning an observable which I had to subscribe to so I could put the data in a variable in my component.
    – loz
    Nov 12 at 19:10











  • Great that will be your first learning curve in angular - Observables are awesome check some rxjs libraries that might help you more in understanding - Check this link [angular.io/guide/rx-library]
    – Rahul
    Nov 12 at 19:14
















1














You cannot directly bind the MVC models into the angular template - fetch the data as JSON using service and then bind the value to it's @Input decorator



Hope this helps - happy coding :)






share|improve this answer




















  • Thanks, I did think I might be approaching this the wrong way. I liked how I had it setup with building the models in an MVC controller but it sounds like I am going to have to fetch the data in an Angular service.
    – loz
    Nov 12 at 15:28










  • Yep you need you access your controller from the angular service and return the data from the controller as JsonResult - check this link - [angular.io/tutorial/toh-pt4]
    – Rahul
    Nov 12 at 15:33










  • I wrote an Angular2 service to get the JSON data from Web API as suggested and it worked. The most difficult thing to understand was that it was returning an observable which I had to subscribe to so I could put the data in a variable in my component.
    – loz
    Nov 12 at 19:10











  • Great that will be your first learning curve in angular - Observables are awesome check some rxjs libraries that might help you more in understanding - Check this link [angular.io/guide/rx-library]
    – Rahul
    Nov 12 at 19:14














1












1








1






You cannot directly bind the MVC models into the angular template - fetch the data as JSON using service and then bind the value to it's @Input decorator



Hope this helps - happy coding :)






share|improve this answer












You cannot directly bind the MVC models into the angular template - fetch the data as JSON using service and then bind the value to it's @Input decorator



Hope this helps - happy coding :)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 15:24









Rahul

9731315




9731315











  • Thanks, I did think I might be approaching this the wrong way. I liked how I had it setup with building the models in an MVC controller but it sounds like I am going to have to fetch the data in an Angular service.
    – loz
    Nov 12 at 15:28










  • Yep you need you access your controller from the angular service and return the data from the controller as JsonResult - check this link - [angular.io/tutorial/toh-pt4]
    – Rahul
    Nov 12 at 15:33










  • I wrote an Angular2 service to get the JSON data from Web API as suggested and it worked. The most difficult thing to understand was that it was returning an observable which I had to subscribe to so I could put the data in a variable in my component.
    – loz
    Nov 12 at 19:10











  • Great that will be your first learning curve in angular - Observables are awesome check some rxjs libraries that might help you more in understanding - Check this link [angular.io/guide/rx-library]
    – Rahul
    Nov 12 at 19:14

















  • Thanks, I did think I might be approaching this the wrong way. I liked how I had it setup with building the models in an MVC controller but it sounds like I am going to have to fetch the data in an Angular service.
    – loz
    Nov 12 at 15:28










  • Yep you need you access your controller from the angular service and return the data from the controller as JsonResult - check this link - [angular.io/tutorial/toh-pt4]
    – Rahul
    Nov 12 at 15:33










  • I wrote an Angular2 service to get the JSON data from Web API as suggested and it worked. The most difficult thing to understand was that it was returning an observable which I had to subscribe to so I could put the data in a variable in my component.
    – loz
    Nov 12 at 19:10











  • Great that will be your first learning curve in angular - Observables are awesome check some rxjs libraries that might help you more in understanding - Check this link [angular.io/guide/rx-library]
    – Rahul
    Nov 12 at 19:14
















Thanks, I did think I might be approaching this the wrong way. I liked how I had it setup with building the models in an MVC controller but it sounds like I am going to have to fetch the data in an Angular service.
– loz
Nov 12 at 15:28




Thanks, I did think I might be approaching this the wrong way. I liked how I had it setup with building the models in an MVC controller but it sounds like I am going to have to fetch the data in an Angular service.
– loz
Nov 12 at 15:28












Yep you need you access your controller from the angular service and return the data from the controller as JsonResult - check this link - [angular.io/tutorial/toh-pt4]
– Rahul
Nov 12 at 15:33




Yep you need you access your controller from the angular service and return the data from the controller as JsonResult - check this link - [angular.io/tutorial/toh-pt4]
– Rahul
Nov 12 at 15:33












I wrote an Angular2 service to get the JSON data from Web API as suggested and it worked. The most difficult thing to understand was that it was returning an observable which I had to subscribe to so I could put the data in a variable in my component.
– loz
Nov 12 at 19:10





I wrote an Angular2 service to get the JSON data from Web API as suggested and it worked. The most difficult thing to understand was that it was returning an observable which I had to subscribe to so I could put the data in a variable in my component.
– loz
Nov 12 at 19:10













Great that will be your first learning curve in angular - Observables are awesome check some rxjs libraries that might help you more in understanding - Check this link [angular.io/guide/rx-library]
– Rahul
Nov 12 at 19:14





Great that will be your first learning curve in angular - Observables are awesome check some rxjs libraries that might help you more in understanding - Check this link [angular.io/guide/rx-library]
– Rahul
Nov 12 at 19:14


















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%2f53265115%2fhow-do-i-pass-a-json-array-to-an-angular2-template-with-input-from-a-asp-net-mv%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







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3