angularjs - 如何在angular ui-router中将对象发送到操作方法

标签 angularjs asp.net-mvc angular-ui-router single-page-application

我正在尝试使用 angular ui-router 制作 SPA。
这是我的 app.js

var productCatalogApp = angular.module('ProductCatalog', ['ui.router']);
productCatalogApp.config(function ($stateProvider, $urlRouterProvider) {  

$stateProvider

    // route to show our basic form (/form)
    .state('wizard', {
        url: '/wizard',
        templateUrl: 'WizardSubForm',
        controller: 'WizardMainController'
    })

    // nested states 
    // each of these sections will have their own view
    // url will be nested (/form/profile)
    .state('wizard.offer', {
        url: '/offer',
        templateUrl: 'OfferForm',
        controller: 'OfferCtrlr'
    })

    // url will be /form/interests
    .state('wizard.customizations', {
        url: '/customizations',
        templateUrl: 'OfferCustomizations',
        controller: 'CustomizationCtrlr',
    });


// catch all route
// send users to the form page 
$urlRouterProvider.otherwise('/wizard');

});

每个状态中的 templateUrl 是操作方法的名称。
这是当我单击 OfferCustomizations 时调用的操作方法。
  public virtual ActionResult OfferCustomizations(string data)
    {
        OfferCustomization offerCustomization = new OfferCustomization();
        //offerCustomization.ProviderId = loginUser.ProviderId;
        offerCustomization.ProductCatalogApiUrl = ProductCatalogApiUrl;
        return View(offerCustomization);
    }

现在我想向 action 方法发送一个对象,但我不知道该怎么做。请帮忙。

最佳答案

Your question is a bit vague , you need to be more specific on what exactly 
you are trying to do.

Generally , this his how you would get data in Angular from the MVC 
application.

In Case of MVC/WebAPI , you should use actions to return JSON result back to 
the angular service which can then be processed by angular. Example below :

app.factory('myService', function($http) {
  var myService = {
    GetData: function() {
      // $http returns a promise, which has a then function, which also 
        returns a promise
      var promise = $http.get('<ActionURL>').then(function (response) {
    // The then function here is an opportunity to modify the response
    console.log(response);
    // The return value gets picked up by the then in the controller.
    return response.data;
     });
      // Return the promise to the controller
      return promise;
    }
  };
  return myService;
});

app.controller('MainCtrl', function( myService,$scope) {
  // Call the async method and then do stuff with what is returned inside 
our own then function
  myService.GetData().then(function(d) {
       $scope.data = d;
  });
});


After this services is called from the MainCtrl , angular will have the data 
from the MVC action available in its $scope.data variable.

关于angularjs - 如何在angular ui-router中将对象发送到操作方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45275470/

相关文章:

javascript - 为什么这个过滤器不输出 IFRAME?

asp.net-mvc - 确定我正在使用的 Kendo UI 版本和产品

javascript - Angular UI-Router 与定义的状态不匹配

javascript - AngularJS 不适用于 Paypal

javascript - 在 1 个 http 请求中解决多个 Promise

angularjs - 如何使用 httpBackend 测试 angularJS 服务?

c# - 如何正确处理子 Action 异常

c# - 您将如何检测 Api Controller 中的当前浏览器?

angularjs - angular ui-router,html5模式总是刷新到/

javascript - angularjs,ui-router访问状态url开头的参数