· 6 years ago · Oct 22, 2019, 01:20 PM
1### Html
2
3```html
4<div ng-app="module-1" ng-init="funcaoInicial()">
5 <!-- Two way data-binding -->
6 <input ng-model="variavel" />
7 <!-- One way data-binding -->
8 <input ng-bind="variavel2" />
9 <select>
10 <option ng-repeat="(key, value) in obj1" value="{{ value }}">{{ key }}</option>
11 </select>
12 <fieldset ng-repeat="value of arr1">
13 <input type="text" value="{{ value }}" readonly />
14 </fieldset>
15</div>
16```
17
18### Javascript
19
20#### Criando um controller (é um *componente* por assim dizer)
21
22```javascript
23angular.module('module-1', ['finance2'])
24.controller('Module1Controller', function Module1Controller() {
25 this.variavel = 1;
26 this.obj1 = {
27 key1: 'value1',
28 key2: 'value2',
29 key3: 'value3'
30 };
31 this.arr1 = [
32 'value1',
33 'value2',
34 'value3',
35 ];
36});
37```
38
39#### Criando e chamando um serviço (de API ou uma utils que criei por exemplo)
40
41> Serviço
42
43```javascript
44angular.module('servico', [])
45.factory('funcaoDoServico', function() {
46 var currencies = ['USD', 'EUR', 'CNY'];
47 var usdToForeignRates = {
48 USD: 1,
49 EUR: 0.74,
50 CNY: 6.09
51 };
52 var convert = function(amount, inCurr, outCurr) {
53 return amount * usdToForeignRates[outCurr] / usdToForeignRates[inCurr];
54 };
55
56 return {
57 currencies: currencies,
58 convert: convert
59 };
60});
61```
62
63> Controller
64
65```javascript
66angular.module('invoice2', ['finance2'])
67.controller('InvoiceController', ['currencyConverter', function InvoiceController(currencyConverter) {
68 this.qty = 1;
69 this.cost = 2;
70 this.inCurr = 'EUR';
71 this.currencies = currencyConverter.currencies;
72
73 this.total = function total(outCurr) {
74 return currencyConverter.convert(this.qty * this.cost, this.inCurr, outCurr);
75 };
76 this.pay = function pay() {
77 window.alert('Thanks!');
78 };
79}]);
80```