gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:01:06 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com