简体   繁体   English

如何在AngularJS Factory中正确声明对象属性?

[英]How to properly declare object properties in AngularJS Factory?

Hi I am creating a custom factory in AngularJS that involves declaring an object that has properties that are used in different pages that are used inside an ng-view/ui-router. 嗨,我正在AngularJS中创建一个自定义工厂,其中涉及声明一个对象,该对象具有在ng-view / ui-router内部使用的不同页面中使用的属性。

app.factory('FormValues', function(){
    //Custom factory that allows values to be passed inbetween different pages in the ng-view form. 
    var formVals = {};    

    formVals.custEmail = '';
    formVals.custPhone = '';
    formVals.movieChosen = '';
    formVals.movieTime = '';
    formVals.movieTickets = '';
    formVals.movieCost = 0;
    formVals.concObjects = {};
    formVals.concPurchases = '';
    formVals.concTotalCost = 0;
    formVals.finalCost = 0;
    formVals.tax = 0;
    formVals.subTotal = 0;

    return formVals;
});

The factory works as intended, the object properties are being saved, and being used in the different pages. 工厂将按预期工作,对象属性将被保存并在不同页面中使用。

But, now I'm pretty sure the way I created this factory and using objects is not the most efficient way. 但是,现在我很确定创建工厂和使用对象的方式并不是最有效的方式。 Any tips on I can clean this up and make it more efficient? 关于我的任何提示可以清理并提高效率? I am not even sure if I am supposed to declare empty, blank object properties. 我什至不确定是否应该声明空的空白对象属性。

Thanks 谢谢

I dont see any cleaning up in the code except a small change to the formVals object. 除了对formVals对象进行少量更改外,我看不到代码中有任何清理内容。

app.factory('FormValues', function(){
    //Custom factory that allows values to be passed inbetween different pages in the ng-view form. 
    var formVals = {
      custEmail : '',
      custPhone : '',
      movieChosen : '',
      movieTime : '',
      movieTickets : '',
      movieCost : 0,
      concObjects : {},
      concPurchases : '',
      concTotalCost : 0,
      finalCost : 0,
      tax: 0,
      subTotal: 0
    };    

    return formVals;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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