简体   繁体   中英

How to load angularjs from MVC view and pass data to angularjs controller?

I have an angularjs MVC application which posts a form to a different domain and a call back comes to MVC controller function as plain http post. When the call back comes back and the view is loaded, it does not run the angularjs controller tied to the view. How can we load the angularjs and use the data in the controller? Thank you!

[System.Web.Http.HttpPost]
public ActionResult Callback()
{
   return View("CallbackView");
}

CallbackView.cshtml

<div ng-controller="CallbackCtrl">
</div>

<script>
    //How to load angularjs?
    //How to read the http post data and use it in the CallbackCtrl?
</script>

AngularJS will only load if the div where you bound your controller (or perhaps better, one of its parent elements) has the ng-app directive.

One approach to reading the post data is to serialize it to JSON in your controller method, then put it in a hidden input element, eg:

<input id="form-data" type="hidden" value="@model.MyFormData" />

From here you can simply use jQueryLite (included in AngularJS) to extract the value. This is certainly not the only way of doing this, just the method that immediately comes to mind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM