6

Below the code of my view (the javascript code is in the view, just temp just for testing).

I'd like assign the ASP.NET MVC model (@Model) to the AngularJS scope ($scope.person)

How can I do this ?

Thanks,

The view

@model MyApp.Person

<script>
var myApp = angular.module('myApp', []);

myApp.controller('personController', ['$scope', '$http', function ($scope, $http) {
    $scope.person = ?????
}]);
</script>

Update 1 : I tried this code, in the JS file :

var myApp = angular.module('myApp', []);

myApp.controller('personController', ['$scope', '$http', function ($scope, $http) {
    $scope.person = @Html.Raw(window.person);
}]);

In the view file :

<script>
    @{
        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    }
    window.person = serializer.Serialize(Model);
</script>

I get 2 errors :

ReferenceError: serializer is not defined (on windows)
window.person = serializer.Serialize(Model);

SyntaxError: illegal character (it's the @)
$scope.person = @Html.Raw(window.person);
1
  • The 'illegal character' is just an 'false' IntelliSense assumption. When surrounded with '' the error will go away. Commented Sep 25, 2014 at 7:59

4 Answers 4

3
<script>
    @{
        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        var json = serializer.Serialize(Model);
    }
    var myApp = angular.module('myApp', []);

    myApp.controller('personController', ['$scope', '$http', function ($scope, $http) {
        $scope.person = @Html.Raw(json);
    }]);
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

Work but only when the code is in the view and not in a separated JS file.
@Kris-I like Satpal said, //Store data in global variable.
in the js file, $scope.person = window.person; in the view file, window.person = @serializer.Serialize(Model); or put this line in the brackets.
Reference error on "ReferenceError: serializer is not defined", in the bracket it's worst.
<script type="text/javascript"> @{ var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); } window.person = @Html.Raw(serializer.Serialize(Model)); console.log(window.person); </script>
3

I am not sure if this will work with Angular.

You can use Json.Encode Method converts a data object to a string that is in the JavaScript Object Notation (JSON) format.

window.person = @Html.Raw(Json.Encode(Model)); //Store data in global variable.

In Your angular code use,

$scope.person = window.person

However, Best solution will be to create a service and fetch the person data using the service.

Complete Code

@model MyApp.Person

<script>
window.person = @Html.Raw(Json.Encode(Model)); //Store data in global variable.
var myApp = angular.module('myApp', []);

myApp.controller('personController', ['$scope', '$http', function ($scope, $http) {
    $scope.person = window.person
}]);
</script>

3 Comments

The question is with angularJS. When I add this line, all the AngularJS code not work anymore.
Same, AngularJS code not work anymore. The best solution is not use AngularJS I think.
In the view, it's temp will be move to separated JS file
0

i had the same problem. this will work. in your view:

<script>
    myApp.value('person', @Html.Raw(Model));
</script>

and in your (angular) controller:

myApp.controller('personController', ['$scope', '$http', 'person' function ($scope, $http, person) {
    console.log(person);
}]);

If you "inject" your json value on your controller, you are sorted.

Comments

0

At first assign value in global java script variable then use it in your separate angular file

 <script>
   var obj =  '@Html.Raw(Model)';
</script>

now in your angular page

$scope.person = obj;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.