Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0120c96

Browse files
#2 add service to resolve promisses
1 parent 90b5f9d commit 0120c96

File tree

4 files changed

+50
-31
lines changed

4 files changed

+50
-31
lines changed

‎angular-materializecss-autocomplete.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
+ '</ng-form>')
3232
}
3333

34-
function autoComplete() {
34+
autoComplete.$inject = ['$q']
35+
function autoComplete($q) {
3536
var directive = {
3637
restrict: 'E',
3738
scope: {
@@ -68,8 +69,13 @@
6869

6970
function modelChanged(model) {
7071
scope.closed = false
71-
scope.itens = scope.func()
7272
callIfDefined(scope.onModelChanged, model)
73+
74+
$q.when(scope.func(model))
75+
.then(function(response){
76+
scope.itens = response
77+
})
78+
7379
}
7480

7581
function iconPrefixDefined() {

‎index.controller.js‎

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,51 @@
1-
(function() {
1+
(function() {
22
'use strict'
33

4-
angular
4+
angular
55
.module('app', ['angularMaterializeAutoComplete'])
66
.controller('IndexController', IndexController)
77

8-
function IndexController(){
9-
var vm = this
10-
11-
var cities = [
12-
{
13-
name: 'Maringa', uf: 'PR', id: '500c709b-9504-462e-9b24-422852cac418'
14-
},
15-
{
16-
name: 'São Paulo', uf: 'SP', id: '6292ad1a-e80f-4642-9bb7-0c19f10db5b1'
17-
},
18-
{
19-
name: 'Apucarana', uf: 'PR', id: '7aebb3b6-c7ae-42aa-8d11-7b5ecc36b6f1'
20-
},
21-
{
22-
name: 'Londrina', uf: 'PR', id: '560c2ebe-b59f-44b2-99a3-19e9f148e401'
23-
},
24-
{
25-
name: 'Curitiba', uf: 'PR', id: '2b97cb57-46a3-4733-a69b-068f0a3b31ab'
26-
}
27-
]
28-
29-
vm.getCities = getCities
8+
IndexController.$inject = ['$http']
9+
function IndexController($http) {
10+
var vm = this
3011

31-
//////////////////////////////
12+
var cities = [
13+
{
14+
name: 'Maringa', uf: 'PR', id: '500c709b-9504-462e-9b24-422852cac418'
15+
},
16+
{
17+
name: 'São Paulo', uf: 'SP', id: '6292ad1a-e80f-4642-9bb7-0c19f10db5b1'
18+
},
19+
{
20+
name: 'Apucarana', uf: 'PR', id: '7aebb3b6-c7ae-42aa-8d11-7b5ecc36b6f1'
21+
},
22+
{
23+
name: 'Londrina', uf: 'PR', id: '560c2ebe-b59f-44b2-99a3-19e9f148e401'
24+
},
25+
{
26+
name: 'Curitiba', uf: 'PR', id: '2b97cb57-46a3-4733-a69b-068f0a3b31ab'
27+
}
28+
]
29+
30+
vm.getDragons = getDragons
31+
32+
function getDragons(search) {
3233

33-
function getCities(){
34-
return cities
34+
console.log('search {}', search)
35+
36+
// não faça isso na vida real, isso é apenas para fins de teste
37+
38+
var request = {
39+
url: 'https://dragons-api.herokuapp.com/api/dragons',
40+
method: 'GET',
3541
}
3642

43+
return $http(request).then(function (response) {
44+
console.log('response {}', response.data.items);
45+
return response.data.items
46+
})
47+
3748
}
49+
50+
}
3851
})()

‎index.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</head>
1111
<body data-ng-controller="IndexController as vm">
1212

13-
<auto-complete model="vm.cidade" func="vm.getCities" property="name" icon-prefix="textsms" label="olosco"></auto-complete>
13+
<auto-complete model="vm.cidade" func="vm.getDragons" property="name" icon-prefix="textsms" label="olosco"></auto-complete>
1414

1515
</body>
1616
<!-- bower:js -->

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-materializecss-autocomplete",
3-
"version": "1.0.0",
3+
"version": "0.0.1",
44
"description": "Angular directive to create autocompletes with [materialize-css](http://materializecss.com/)",
55
"main": "angular-materializecss-autocomplete.js",
66
"scripts": {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /