Skip to content

Instantly share code, notes, and snippets.

@trungk18
Last active April 25, 2018 14:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Pass data from MVC to Angular 2

Index.cshtml

<my-angular-app appID="@Model.AppId">Loading...</my-angular-app> 

util.ts

//function to get the attribute value.
export function getAttribute(app: string, attributeName: string) {
  return document.getElementsByTagName(app)[0].getAttribute(attributeName);
}

app.component.ts

import { getAttribute } from './util'
export class AppComponent implements OnInit {
  ngOnInit(){
    let appId = getAttribute("my-angular-app", "appID")
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment