[英]AngularJS - How to create a backend?
我在一个html页面上有一个angularjs应用程序,并且效果很好。 但是,我不想在angularjs脚本上使用常量,而是想让它动态化,以便有人可以在另一个页面上更改值。 这是一些我想动态化的示例代码:
// compute base_cost
$scope.base_cost = function(pages, delivery) {
// change this if you want to change price per page
price_per_page = 150;
// change this if you want to change the cost of a 3 day delivery
three_business_days = 100;
base_cost = pages * price_per_page;
if (delivery === "3 Business Days"){
base_cost = pages * price_per_page + three_business_days;
}
return base_cost;
};
我希望price_per_page
和three_business_days
有一个单独的页面(可能受密码保护),客户可以在要更改价格时更改这些变量的常量,等等。
最简单的方法是什么?
附言:我目前正在寻找有关它的信息,但只需一点指导即可。
谢谢
假设您要在会话中保留这些值,您不仅需要服务,还需要:一种API /数据库。
有无数的选择:node.js,ASP.NET Web API等。取决于您的经验。
您是否考虑过使用Firebase来快速启动和运行某些东西(假设您没有现有基础架构可依靠)? 有一个合理的免费试用层。
AngularJS也有一些不错的支持:
https://www.firebase.com/docs/web/libraries/angular/index.html
但是,实时方面可能会显得过分。
您所描述的是工厂/服务。 它是MVC应用程序中的model
。
从角度上讲,工厂是单例,您无需实例化它的新副本,并且在一个区域中对其所做的更改会一直保留到站点的另一个区域。 当然,仅在会话期间,您需要将模型状态存储到其他某种形式的存储中。
这是一个示例应用程序,它具有一个控制器(展示如何使用它)和一个工厂。
angular.module('testModule', [])
.factory('BaseCost', function(){
// change this if you want to change price per page
price_per_page = 150;
// change this if you want to change the cost of a 3 day delivery
three_business_days = 100;
return {
price: function(amt){
price_per_page = amt;
},
days: function(cost){
three_business_dates = cost;
},
base: function(pages){
return pages * price_per_page;
}
};
})
.controller('ACtrl',function($scope, BaseCost){
$scope.setPrice = function(amt){
BaseCost.price(amt);
};
$scope.daysCost = function(cost){
BaseCost.days(cost);
}
$scope.baseCost = BaseCost.base(pages);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.