with
Ionicframework and Angular
Satit Rianpit
rianpit@gmail.com
23-27 May 2016
https://www.npmjs.com/
1. Java SDK
2. Node.js
3. Python (for libsass, set Python path for CLI)
4. Node Packages
# npm install -g ionic cordova
for osx
# npm install -g ionic cordova ios-sim
// Other pacakges
# npm install -g gulp bower http-server
# ionic start appName tabs
# cd appName
# ionic serve --lab
// Theme Customize
# ionic setup sass
<html>
<title>Hello world</title>
<script src="path/to/angular.js"></script>
<body ng-app>
Your name:
<input type="text" ng-model="name" />
<span>{{ name }}</span>
</body>
</html><html ng-app ng-init="name='Satit'">
Your name:
<span>{{ name }}</span>
<!-- use directive -->
<h1 ng-bind="name"></h1>
One-way data binding.
Your name:
<input type="text" ng-model="name" />
<span>{{ name }}</span>
<!-- use directive -->
<h1 ng-bind="name"></h1>
Two-way data binding.
angular.controller('MainCtrl', function ($scope) {
$scope.name = 'Satit Rianpit';
});controller.js
app.html
<script>
angular.module('app', [])
.controller('MainController', function ($scope) {
//bind data to view
$scope.name = 'Satit Rianpit';
// bind to function
$scope.showName = function () {
alert($scope.name);
};
});
</script>
<body ng-app="app" ng-controller="MainController">
Your name:
<input type="text" ng-model="name" />
<button type="button" ng-click="showName()">Show me.</button>
</body>var app = angular.module('app', []);
app.controller('MainController', function ($scope) { ... });Or
<script src="MainController.js"></script>
<!-- App.jade -->
script(src="MainController.js")
button(type="button" ng-show="isShow", style="background-color: red")
| Welcome to AngularJS
button(type="button", ng-click="toggleMe()") Show/Hide// MainController.js
app.controller('MainController', function ($scope) {
$scope.isShow = true;
$scope.toggleMe = function () {
$scope.isShow = !$scope.isShow;
};
});
input(type="text", ng-model="query")
ul
li(ng-repeat="p in products | filter: query")
| {{ p.name }} [{{ p.qty }}]angular.moduel('app', [])
.controller('MainController', function ($scope) {
$scope.products = [
{ name: 'Apple Watch', qty: 10 },
{ name: 'iPad', qty: 5 }
{ name: 'iPhone', qty: 15 },
...
];
});MainController.js
App.jade
ul
li(ng-repeat="p in products")
span(ng-if="p.qty < 10", style="color: red;") {{ p.name }} [{{ p.qty }}]
span(ng-if="p.qty >= 10", style="color: green;") {{ p.name }} [{{ p.qty }}]angular.moduel('app', [])
.controller('MainController', function ($scope) {
$scope.products = [
{ name: 'Apple Watch', qty: 10 },
{ name: 'iPad', qty: 5 }
{ name: 'iPhone', qty: 15 },
...
];
});MainController.js
App.jade
doctype html
html
head
title Directive
script(src="../vendor/angular/angular.js")
script(src="Directive.js")
body(ng-app="app")
hello(name="Satit Rianpit")
angular.module('app', [])
.directive('hello', function () {
return {
restrict: 'E', // E = element, A = attribute, C = class
template: '<div>Hello, {{ name }}</div>',
scope: {
name: '@'
}
};
});App.jade
Directive.js
// script
$scope.price = 10000;
$scope.name = 'computer';
// jade
price: {{ price | number }} // result: 10,000
name: {{ name | uppercase }} // result: COMPUTERhttps://docs.angularjs.org/api/ng/filter
// filter.js
app.filter('toSexName', function () {
return function (sex) {
return sex == '1' ? 'ชาย' : sex == '2' ? 'หญิง' : 'ไม่ทราบ';
};
});
// page.html
เพศ: {{ sex | toSexName }}app.factory('factoryName', function () {
return {
all: function () {
// query database
return rows;
},
search: function (query) {
// query database
return rows;
}
};
});app.factory('Api', function ($q, $http) {
return {
all: function () {
$http({ url: '/apis/all', method: 'post', data: { id: 1 }})
.success(function (data, status) {
// success
})
.error(function (data, status) {
// error
});
}
};
});