<!DOCTYPE html>
<html>
<head>
<script src="https://code.angularjs.org/1.4.8/angular.js"></script>
<script src="myapplication.js"></script>
</head>
<body ng-app="myFirstApplication">
<div ng-controller="MyFirstController as vm">
<p>Current name: <strong>{{vm.name}}</strong></p>
<p>
Set new name:
<input type="text" ng-model="vm.newName">
<button type="submit" ng-click="vm.setName()">Set name</button>
</p>
</div>
</body>
</html>
(function () {
//Create application
angular.module('myFirstApplication', []);
//Create controller within application
angular.module('myFirstApplication').controller("MyFirstController", [myFirstController]);
function myFirstController() {
var viewModel = this;
//Model
viewModel.name = "Robert";
viewModel.newName = "";
//Functions
viewModel.setName = function() {
viewModel.name = viewModel.newName;
}
}
})();