--- author: "Jim Bennett" categories: ["Visual Studio", "Cordova", "Technology", "Mac", "Development", "iOS", "Using Visual Studio to develop Cordova apps", "angular", "topcoat"] date: 2014-05-27T00:10:02Z description: "" draft: false slug: "using-visual-studio-to-develop-cordova-apps-part-2-angular" tags: ["Visual Studio", "Cordova", "Technology", "Mac", "Development", "iOS", "Using Visual Studio to develop Cordova apps", "angular", "topcoat"] title: "Using Visual Studio to develop Cordova apps part 2 - Creating an app" images: - /blogs/using-visual-studio-to-develop-cordova-apps-part-2-angular/banner.png featured_image: banner.png --- Building on [the previous post in this series](http://jimbobbennett.ghost.io/using-visual-studio-to-develop-cordova-apps/), I thought it would be fun to try to learn [Angular.js](https://angularjs.org/) and include this in my Cordova app. I could also do with so nice looking widgets, so I decided to have a play with [TopCoat](http://topcoat.io/). First thing I need is an app to build. One of my current 'First World Problems' is having to convert from US units (mg/dL) to international blood sugar units (mmol/L). I'm a diabetic and I've been living most of my life in the UK and in Hong Kong - two countries that use international units for blood sugar measurement. I'm now living in Bermuda, and they only sell US spec blood sugar meters, and provide testing strips that only work with these small set of meters. The meters are factory locked to US units for some reason (unlike international meters that can be changed). So for now, I'm stuck with testing in US units. The conversion is 18 to 1 - so a US measurement of 90 is the same as an international measurement of 5. Simple enough, but I've got better things to do than learn my 18 times table, like create an app to do the conversion for me. So, lets create an app. ## Building the app I'm starting with the simple HelloWorld example from the previous post, and changing it to suit my needs. The UI is really simple - a top bar and two input boxes.  The top bar is the TopCoat navigation bar ```HTML
``` This gives a nice header bar. I'm using the TopCoat mobile dark skin, I really like the way it looks. For the input boxes I wanted to select all when you press them - that way you can overtype instead of having to delete the existing contents. A quick search of Stack Overflow gives a few suggestions to select everything, but these lead to a problem. When the text is selected the iPhone pops up a menu with standard options for selected text, Cut, Copy etc. This is far from ideal as it looks really bad. In the end, I just set the value to undefined on the click event handler. This clears the box so there is nothing to select allowing you to type the number without having to delete everything. The fun part with the input boxes was the conversion. Rather than having to click a button to convert, I wanted it to happen on the fly. This is where Angular comes in. Angular allows you to bind the values from input boxes to variables - you update the input, the variable automatically updates, you update a variable, the field showing it automatically updates. It also provides events to detect value changes. So first off, a controller. ```Javascript // Create the angular module var bloodSugarConversionApp = angular.module('BloodSugarConversionApp', []); // Create the controller bloodSugarConversionApp.controller('BloodSugarConversionCtrl', function ($scope) { // defines the initial values for our variables $scope.usMeasurement = 0; $scope.internationalMeasurement = 0; // this function is wired up to a change handler for the US input field // when this changes, it updates the international measurement $scope.changeUSMeasurement = function() { // setting the international measurement updates the value in the input $scope.internationalMeasurement = Math.round(($scope.usMeasurement / 18) * 10) / 10; }; // this function is wired up to a change handler for the international input field // when this changes, it updates the US measurement $scope.changeInternationalMeasurement = function() { // setting the US measurement updates the value in the input $scope.usMeasurement = Math.round(($scope.internationalMeasurement * 18) * 10) / 10; }; }); ``` So, what's going on here. First we define a callback that creates the *BloodSugarConversionCtrl* controller. This function sets up the *$scope* to have a couple of variables for the two units of measurement initialised to 0, and declares a couple of functions on it that will convert when a value changes. This controller is then wired up in our HTML. ```HTMLmg/dL
mmol/L