Skip to content

Instantly share code, notes, and snippets.

@trungvose
Last active April 25, 2018 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trungvose/2844eb41821998b0613798923d35a317 to your computer and use it in GitHub Desktop.
Save trungvose/2844eb41821998b0613798923d35a317 to your computer and use it in GitHub Desktop.
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