How to pass @Model to Angular ng-init

Can't pass @Model in ng-initin ASP.NET MVC 4 view? When I do it like data-ng-init=init(@Model)this is undefined in the function init().

$scope.init = fuynction(model){
    console.log(model); // prints undefined
}

By the way, I am new to AngularJs. Any help is appreciated in advance.

+3
source share
3 answers

Serialize the model into a json string using the Newtonsoft.Json assembly or similar, and pass the serialization result to data-ng-init.

+5
source

As I said, you need to translate between your server objects (for example @Model) and objects on the client side.

For example, if you just wanted to use a property Name @Model, you could do something like this:

<div ng-init="init('@Model.Name')"></div>

$scope.init = function(name){
    console.log(name); // prints value of name
}

, @Model.Name JavaScript ( ).

, @Model. .

, $http .

+17

@using ProjectApp.Models, data-ng-init = "init (@Newtonsoft.Json.JsonConvert.SerializeObject(Model))"

+2

All Articles