Skip to content

Latest commit

 

History

History
75 lines (56 loc) · 1.62 KB

README.Controllers.md

File metadata and controls

75 lines (56 loc) · 1.62 KB

Creating an AngularJS Controller

public class ShoppingCartController 
{         
    public ShoppingCartController(Scope _scope, Timeout _timeout)
    {
	 }
}

any other parameter in the class constructor will be treated as an injectable parameter (in the example above the $timeout object is injected). If you want to use any injected parameter outside of the constructor, save its reference to a field variable.

The class should also declare the objects that are part of the "model". What you refer from outside C# (e.g. HTML) should be declared with the public modifier. For example:

public class ShoppingCartController : Scope
{     
    public List<CartItem> items;
    public double billDiscount;

    public ShoppingCartController(Scope _scope, Timeout _timeout)
    {
    }
}

After the class controller is defined you need to register it within the application-module:

app.Controller<ShoppingCartController>();                                  

Implementing the controller logic

Controller functions are defined as methods, but you can also define them as Action or Func:

public void remove(int index)
{
	items.RemoveAt(index);
}

or (within the constructor):

remove = (index) => 
{
	items.RemoveAt(index);
};

Adding a watch

A watch keeps on listening to a function, and when its value changes another function is called.

Put the watch in the constructor:

_scope.Watch<double>(()=>totalCart, calculateDiscount);

and then define:

public double calculateDiscount(double newValue, double oldValue)
{
	billDiscount = newValue > 100 ? 10 : 0;
};