加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何在角度ui-router中将对象发送到action方法

发布时间:2020-12-17 16:59:44 所属栏目:安全 来源:网络整理
导读:我正在尝试使用角度ui路由器制作SPA. 这是我的app.js. var productCatalogApp = angular.module('ProductCatalog',['ui.router']);productCatalogApp.config(function ($stateProvider,$urlRouterProvider) { $stateProvider // route to show our basic for
我正在尝试使用角度ui路由器制作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是action方法的名称.
这是我点击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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读