0

I have an angularjs app on one html page and it pretty much works well. But instead of using constants on a angularjs script, I'd like to make it dynamic where someone can change the values from another page. Here's some sample code I'd like to be dynamic:

// 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;
};

I'd like the price_per_page and three_business_days to have a seperate page (maybe password-protected) where these variables' constants can be changed by the client when he wants to change the price, etc.

What is the simplest way to do this?

P.S. I'm already currently searching about it, but just a bit of guidance would be greatly appreciated.

Thanks

Santosh Panda
  • 7,235
  • 8
  • 43
  • 56
Pau
  • 528
  • 1
  • 3
  • 10
  • This is a very complex question which cannot be answered. – hgoebl Oct 25 '14 at 06:13
  • Just any terms to google or hints on how to connect the dots is all i ask, please. – Pau Oct 25 '14 at 06:27
  • It's as simple as creating a form page with many inputs ( which should be dynamically generated by PHP or some such language ) and processing that page to either a database or a simple .csv file. Even a text file will work, just encode everything into an array and decode when reading. On page load, read the file or database information and repopulate all the form. Simple ( but tedious work ). – Gary Hayes Oct 25 '14 at 06:59

3 Answers3

0

Assuming another page means another view, see this answer. Basically, create a service.

Community
  • 1
  • 1
Justin Poehnelt
  • 2,992
  • 1
  • 19
  • 23
0

Assuming you want to persist these values across sessions you need more than just a service: you need some kind of API / database.

There are endless choices: node.js, ASP.NET Web API, etc. Depends on your experience.

Have you considered firebase to get something up and running quickly (assuming you don't have existing infrastructure to rely on)? There's a reasonable free tier for experimentation.

https://www.firebase.com

There's also some nice AngularJS support:

https://www.firebase.com/docs/web/libraries/angular/index.html

The real time aspect may be overkill though..

Nat Wallbank
  • 1,377
  • 12
  • 12
0

What you are describing is a factory / service. It is the model in an MVC application.

In angular, a factory is a singleton, you don't instantiate new copies of it, and changes made to it in one area persist through to another area of site. Of course, that is only during the life of your session, you'd need to store the models state to some other form of storage.

Here is an example application, it has a controller (to show how to use it), and a factory.

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);

});
Gabs00
  • 1,869
  • 1
  • 13
  • 12