Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

By the way I'm new to AngularJs. Any help is appreciated in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
701 views
Welcome To Ask or Share your Answers For Others

1 Answer

As I commented, you need to translate between your server-side objects (e.g. @Model) and client-side objects.

For example, if you just wanted to use the Name property of @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
}

Notice, that even in this simple example, it is necessary to format @Model.Name into a JavaScript string (by putting quotes around it).

Probably, you don't want to use the entire @Model object. It is best to choose only the fields you want and pass them in individually.

If you need access to a lot of data from the server, it is recommended to use $http service and make a server call for the data.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...