SlideShare une entreprise Scribd logo
1  sur  4
Télécharger pour lire hors ligne
Componente de Busca
com
AngularJS

Autor: Douglas Lira
https://github.com/douglaslira/angularjs

douglas.lira.web@gmail.com | angularjs-br@googlegroups.com
Ajude a compartilhar conhecimento!!
1 – A idéia
Simplesmente criar um componente de busca que seja utilizado com qualquer controller.

2 – Ferramentas
AngularJS - http://angularjs.org/

3 – Mão na massa!!
1 – Arquivo index.html – index.html
<html ng-app="App">
<head>
<linkhref="css/bootstrap.min.css" rel="stylesheet" />
<script src="js/libs/angular.min.js"></script>
<script src="js/search.js"></script>
<script src=”js/UserController.js”></script>
</head>
<body>
<div ng-controller="UserController" style="margin: 20px">
<h1>Pesquisar por usuário</h1>
<search-box search-text="userSearchText" search-for="searchForUser"></search-box>
<search-results
search-for="searchForUser"
search-results="userSearchResults"
search-text="userSearchText">
</search-results>
</div>
</body>
</html>

2 – Arquivo search.js – js/search.js
angular.module("App", []).directive('searchBox', function() {
return {
restrict: 'E',
transclude: false,
scope: {
searchText: '=',
searchFor: '='
},
controller: function($scope) {
$scope.localSearchText = '';
$scope.clearSearch = function() {
$scope.searchText = "";
$scope.localSearchText = "";
};
$scope.doSearch = function() {
$scope.searchText = $scope.localSearchText;
};
},
replace: true,
template:
'<form>' +
'<div>' +
'<input ng-model="localSearchText" type="text" />' +
'</div>' +
'<div>' +
'<button ng-click="clearSearch()" class="btn btn-small">Limpar</button>' +
'<button ng-click="doSearch()" class="btn btn-small">Pesquisar</button>' +
'</div> ' +
'<div ng-show="searchFor">' +
'<img ng-show="searchFor" src="img/loading.gif" /> ' +
'Pesquisando...' +
'</div>' +
'</form>'
};
Continuando o arquivo search.js...
}).directive('searchResults', function() {
return {
restrict: 'E',
transclude: true,
scope: {
searchFor: '=',
searchResults: '=',
searchText: '='
},
replace: true,
template:
'<div ng-hide="searchFor">' +
'<h4 ng-show="searchResults">Foram encontrado(s) {{searchResults.length}} resultado(s) com o
texto: "{{searchText}}"</h4>' +
'<ul ng-show="searchResults">' +
'<li ng-repeat="searchResult in searchResults | filter:x">' +
'{{searchResult}}' +
'</li>' +
'</ul>' +
'</div>'
};
}).filter('checkSource',function(){
return function(items,searchText) {
var filtered = [];
for (var i = 0; i < items.length; i++) {
if (items[i].indexOf(searchText) != -1){
filtered.push(items[i]);
}
}
return filtered;
};
});

3 – Arquivo UserController.js – js/UserController.js
function UserController($scope, $timeout, $filter) {
$scope.$watch("userSearchText", function(userSearchText) {
if(userSearchText) {
$scope.userSearchSource = ['Douglas','Jean','Klederson','Rodrigo','Taller'];
$scope.userSearchResults = [];
$scope.searchingResult = true;
$timeout(function() {
$scope.searchingResult = false;
$scope.userSearchResults
=
$filter('checkSource')
($scope.userSearchSource,userSearchText);
}, 1000);
} else {
$scope.searchingResult = false;
$scope.userSearchResults = [];
}
});
}
Bom é isso ae... até aproxima abraços ;)

Contenu connexe

Tendances

SES Toronto 2008; Joe Dolson
SES Toronto 2008; Joe DolsonSES Toronto 2008; Joe Dolson
SES Toronto 2008; Joe DolsonJoseph Dolson
 
Angular JS Routing
Angular JS RoutingAngular JS Routing
Angular JS Routingkennystoltz
 
Requirejs
RequirejsRequirejs
Requirejssioked
 
jQuery Mobile Introduction ( demo on EZoapp )
jQuery Mobile Introduction ( demo on EZoapp )jQuery Mobile Introduction ( demo on EZoapp )
jQuery Mobile Introduction ( demo on EZoapp )EZoApp
 
Dive into AngularJS Routing
Dive into AngularJS RoutingDive into AngularJS Routing
Dive into AngularJS RoutingEgor Miasnikov
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing optionsNir Kaufman
 
J Query (Complete Course) by Muhammad Ehtisham Siddiqui
J Query (Complete Course) by Muhammad Ehtisham SiddiquiJ Query (Complete Course) by Muhammad Ehtisham Siddiqui
J Query (Complete Course) by Muhammad Ehtisham SiddiquiMuhammad Ehtisham Siddiqui
 
Be mean
Be meanBe mean
Be meanSuissa
 
JoomlaEXPO Presentation by Joe LeBlanc
JoomlaEXPO Presentation by Joe LeBlancJoomlaEXPO Presentation by Joe LeBlanc
JoomlaEXPO Presentation by Joe LeBlancJohn Coonen
 
Joomlapresentation
JoomlapresentationJoomlapresentation
Joomlapresentationjlleblanc
 
Lightning Talk: Making JS better with Browserify
Lightning Talk: Making JS better with BrowserifyLightning Talk: Making JS better with Browserify
Lightning Talk: Making JS better with Browserifycrgwbr
 
How routing works in angular js
How routing works in angular jsHow routing works in angular js
How routing works in angular jscodeandyou forums
 

Tendances (20)

Canjs
CanjsCanjs
Canjs
 
SES Toronto 2008; Joe Dolson
SES Toronto 2008; Joe DolsonSES Toronto 2008; Joe Dolson
SES Toronto 2008; Joe Dolson
 
Canjs
CanjsCanjs
Canjs
 
Angular JS Routing
Angular JS RoutingAngular JS Routing
Angular JS Routing
 
Requirejs
RequirejsRequirejs
Requirejs
 
Xxx
XxxXxx
Xxx
 
jQuery Mobile Introduction ( demo on EZoapp )
jQuery Mobile Introduction ( demo on EZoapp )jQuery Mobile Introduction ( demo on EZoapp )
jQuery Mobile Introduction ( demo on EZoapp )
 
Dive into AngularJS Routing
Dive into AngularJS RoutingDive into AngularJS Routing
Dive into AngularJS Routing
 
Sails.js - Overview
Sails.js - OverviewSails.js - Overview
Sails.js - Overview
 
test
testtest
test
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
 
Requirejs
RequirejsRequirejs
Requirejs
 
J Query (Complete Course) by Muhammad Ehtisham Siddiqui
J Query (Complete Course) by Muhammad Ehtisham SiddiquiJ Query (Complete Course) by Muhammad Ehtisham Siddiqui
J Query (Complete Course) by Muhammad Ehtisham Siddiqui
 
Be mean
Be meanBe mean
Be mean
 
UI-Router
UI-RouterUI-Router
UI-Router
 
JoomlaEXPO Presentation by Joe LeBlanc
JoomlaEXPO Presentation by Joe LeBlancJoomlaEXPO Presentation by Joe LeBlanc
JoomlaEXPO Presentation by Joe LeBlanc
 
Joomlapresentation
JoomlapresentationJoomlapresentation
Joomlapresentation
 
Rails3 asset-pipeline
Rails3 asset-pipelineRails3 asset-pipeline
Rails3 asset-pipeline
 
Lightning Talk: Making JS better with Browserify
Lightning Talk: Making JS better with BrowserifyLightning Talk: Making JS better with Browserify
Lightning Talk: Making JS better with Browserify
 
How routing works in angular js
How routing works in angular jsHow routing works in angular js
How routing works in angular js
 

Similaire à Criando um componente de busca com AngularJS

intro to Angular js
intro to Angular jsintro to Angular js
intro to Angular jsBrian Atkins
 
Ionic으로 모바일앱 만들기 #3
Ionic으로 모바일앱 만들기 #3Ionic으로 모바일앱 만들기 #3
Ionic으로 모바일앱 만들기 #3성일 한
 
AngularJS for Legacy Apps
AngularJS for Legacy AppsAngularJS for Legacy Apps
AngularJS for Legacy AppsPeter Drinnan
 
AngularJS interview questions
AngularJS interview questionsAngularJS interview questions
AngularJS interview questionsUri Lukach
 
AngularJS: an introduction
AngularJS: an introductionAngularJS: an introduction
AngularJS: an introductionLuigi De Russis
 
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJSAngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJSmurtazahaveliwala
 
Introduction of angular js
Introduction of angular jsIntroduction of angular js
Introduction of angular jsTamer Solieman
 
Course CodeSchool - Shaping up with Angular.js
Course CodeSchool - Shaping up with Angular.jsCourse CodeSchool - Shaping up with Angular.js
Course CodeSchool - Shaping up with Angular.jsVinícius de Moraes
 
angularjs_tutorial.docx
angularjs_tutorial.docxangularjs_tutorial.docx
angularjs_tutorial.docxtelegramvip
 
Angular workshop
Angular workshopAngular workshop
Angular workshophoa long
 
Parse Apps with Ember.js
Parse Apps with Ember.jsParse Apps with Ember.js
Parse Apps with Ember.jsMatthew Beale
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosAngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosLearnimtactics
 
243329387 angular-docs
243329387 angular-docs243329387 angular-docs
243329387 angular-docsAbhi166803
 
jQuery - the world's most popular java script library comes to XPages
jQuery - the world's most popular java script library comes to XPagesjQuery - the world's most popular java script library comes to XPages
jQuery - the world's most popular java script library comes to XPagesMark Roden
 
GDG Atlanta - Angular.js Demo and Workshop
GDG Atlanta - Angular.js Demo and WorkshopGDG Atlanta - Angular.js Demo and Workshop
GDG Atlanta - Angular.js Demo and WorkshopDrew Morris
 

Similaire à Criando um componente de busca com AngularJS (20)

Angular js
Angular jsAngular js
Angular js
 
intro to Angular js
intro to Angular jsintro to Angular js
intro to Angular js
 
Ionic으로 모바일앱 만들기 #3
Ionic으로 모바일앱 만들기 #3Ionic으로 모바일앱 만들기 #3
Ionic으로 모바일앱 만들기 #3
 
AngularJS for Legacy Apps
AngularJS for Legacy AppsAngularJS for Legacy Apps
AngularJS for Legacy Apps
 
Custom directive and scopes
Custom directive and scopesCustom directive and scopes
Custom directive and scopes
 
AngularJS interview questions
AngularJS interview questionsAngularJS interview questions
AngularJS interview questions
 
AngularJS: an introduction
AngularJS: an introductionAngularJS: an introduction
AngularJS: an introduction
 
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJSAngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
 
AngularJS By Vipin
AngularJS By VipinAngularJS By Vipin
AngularJS By Vipin
 
Introduction of angular js
Introduction of angular jsIntroduction of angular js
Introduction of angular js
 
Course CodeSchool - Shaping up with Angular.js
Course CodeSchool - Shaping up with Angular.jsCourse CodeSchool - Shaping up with Angular.js
Course CodeSchool - Shaping up with Angular.js
 
angularjs_tutorial.docx
angularjs_tutorial.docxangularjs_tutorial.docx
angularjs_tutorial.docx
 
Angular workshop
Angular workshopAngular workshop
Angular workshop
 
Angular js
Angular jsAngular js
Angular js
 
Parse Apps with Ember.js
Parse Apps with Ember.jsParse Apps with Ember.js
Parse Apps with Ember.js
 
Angular js slides
Angular js slidesAngular js slides
Angular js slides
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosAngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
 
243329387 angular-docs
243329387 angular-docs243329387 angular-docs
243329387 angular-docs
 
jQuery - the world's most popular java script library comes to XPages
jQuery - the world's most popular java script library comes to XPagesjQuery - the world's most popular java script library comes to XPages
jQuery - the world's most popular java script library comes to XPages
 
GDG Atlanta - Angular.js Demo and Workshop
GDG Atlanta - Angular.js Demo and WorkshopGDG Atlanta - Angular.js Demo and Workshop
GDG Atlanta - Angular.js Demo and Workshop
 

Plus de Douglas Lira

AngularJS sem medo
AngularJS sem medoAngularJS sem medo
AngularJS sem medoDouglas Lira
 
Directive com AngularJS - Datepicker
Directive com AngularJS - DatepickerDirective com AngularJS - Datepicker
Directive com AngularJS - DatepickerDouglas Lira
 
Autenticação com AngularJS e loadOnDemand
Autenticação com AngularJS e loadOnDemandAutenticação com AngularJS e loadOnDemand
Autenticação com AngularJS e loadOnDemandDouglas Lira
 
AngularJS com loadOnDemand
AngularJS com loadOnDemandAngularJS com loadOnDemand
AngularJS com loadOnDemandDouglas Lira
 
AngularJS com NODE.JS e Socket.IO
AngularJS com NODE.JS e Socket.IOAngularJS com NODE.JS e Socket.IO
AngularJS com NODE.JS e Socket.IODouglas Lira
 
Mude seu jeito de pensar com MongoDB
Mude seu jeito de pensar com MongoDBMude seu jeito de pensar com MongoDB
Mude seu jeito de pensar com MongoDBDouglas Lira
 
Menu dinâmico com kendoUI
Menu dinâmico com kendoUIMenu dinâmico com kendoUI
Menu dinâmico com kendoUIDouglas Lira
 

Plus de Douglas Lira (9)

AngularJS sem medo
AngularJS sem medoAngularJS sem medo
AngularJS sem medo
 
Directive com AngularJS - Datepicker
Directive com AngularJS - DatepickerDirective com AngularJS - Datepicker
Directive com AngularJS - Datepicker
 
Autenticação com AngularJS e loadOnDemand
Autenticação com AngularJS e loadOnDemandAutenticação com AngularJS e loadOnDemand
Autenticação com AngularJS e loadOnDemand
 
AngularJS com loadOnDemand
AngularJS com loadOnDemandAngularJS com loadOnDemand
AngularJS com loadOnDemand
 
AngularJS com NODE.JS e Socket.IO
AngularJS com NODE.JS e Socket.IOAngularJS com NODE.JS e Socket.IO
AngularJS com NODE.JS e Socket.IO
 
Time sheet
Time sheetTime sheet
Time sheet
 
Mude seu jeito de pensar com MongoDB
Mude seu jeito de pensar com MongoDBMude seu jeito de pensar com MongoDB
Mude seu jeito de pensar com MongoDB
 
Menu dinâmico com kendoUI
Menu dinâmico com kendoUIMenu dinâmico com kendoUI
Menu dinâmico com kendoUI
 
Plugin zend acl
Plugin zend aclPlugin zend acl
Plugin zend acl
 

Dernier

An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPCeline George
 
4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptxmary850239
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfChristalin Nelson
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxryandux83rd
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
The Emergence of Legislative Behavior in the Colombian Congress
The Emergence of Legislative Behavior in the Colombian CongressThe Emergence of Legislative Behavior in the Colombian Congress
The Emergence of Legislative Behavior in the Colombian CongressMaria Paula Aroca
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...Nguyen Thanh Tu Collection
 
Unit :1 Basics of Professional Intelligence
Unit :1 Basics of Professional IntelligenceUnit :1 Basics of Professional Intelligence
Unit :1 Basics of Professional IntelligenceDr Vijay Vishwakarma
 
physiotherapy in Acne condition.....pptx
physiotherapy in Acne condition.....pptxphysiotherapy in Acne condition.....pptx
physiotherapy in Acne condition.....pptxAneriPatwari
 
Shark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristicsShark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristicsArubSultan
 

Dernier (20)

Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERP
 
4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdf
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Chi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical VariableChi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical Variable
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptx
 
Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
The Emergence of Legislative Behavior in the Colombian Congress
The Emergence of Legislative Behavior in the Colombian CongressThe Emergence of Legislative Behavior in the Colombian Congress
The Emergence of Legislative Behavior in the Colombian Congress
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
 
Unit :1 Basics of Professional Intelligence
Unit :1 Basics of Professional IntelligenceUnit :1 Basics of Professional Intelligence
Unit :1 Basics of Professional Intelligence
 
CARNAVAL COM MAGIA E EUFORIA _
CARNAVAL COM MAGIA E EUFORIA            _CARNAVAL COM MAGIA E EUFORIA            _
CARNAVAL COM MAGIA E EUFORIA _
 
physiotherapy in Acne condition.....pptx
physiotherapy in Acne condition.....pptxphysiotherapy in Acne condition.....pptx
physiotherapy in Acne condition.....pptx
 
Shark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristicsShark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristics
 

Criando um componente de busca com AngularJS

  • 1. Componente de Busca com AngularJS Autor: Douglas Lira https://github.com/douglaslira/angularjs douglas.lira.web@gmail.com | angularjs-br@googlegroups.com Ajude a compartilhar conhecimento!!
  • 2. 1 – A idéia Simplesmente criar um componente de busca que seja utilizado com qualquer controller. 2 – Ferramentas AngularJS - http://angularjs.org/ 3 – Mão na massa!!
  • 3. 1 – Arquivo index.html – index.html <html ng-app="App"> <head> <linkhref="css/bootstrap.min.css" rel="stylesheet" /> <script src="js/libs/angular.min.js"></script> <script src="js/search.js"></script> <script src=”js/UserController.js”></script> </head> <body> <div ng-controller="UserController" style="margin: 20px"> <h1>Pesquisar por usuário</h1> <search-box search-text="userSearchText" search-for="searchForUser"></search-box> <search-results search-for="searchForUser" search-results="userSearchResults" search-text="userSearchText"> </search-results> </div> </body> </html> 2 – Arquivo search.js – js/search.js angular.module("App", []).directive('searchBox', function() { return { restrict: 'E', transclude: false, scope: { searchText: '=', searchFor: '=' }, controller: function($scope) { $scope.localSearchText = ''; $scope.clearSearch = function() { $scope.searchText = ""; $scope.localSearchText = ""; }; $scope.doSearch = function() { $scope.searchText = $scope.localSearchText; }; }, replace: true, template: '<form>' + '<div>' + '<input ng-model="localSearchText" type="text" />' + '</div>' + '<div>' + '<button ng-click="clearSearch()" class="btn btn-small">Limpar</button>' + '<button ng-click="doSearch()" class="btn btn-small">Pesquisar</button>' + '</div> ' + '<div ng-show="searchFor">' + '<img ng-show="searchFor" src="img/loading.gif" /> ' + 'Pesquisando...' + '</div>' + '</form>'
  • 4. }; Continuando o arquivo search.js... }).directive('searchResults', function() { return { restrict: 'E', transclude: true, scope: { searchFor: '=', searchResults: '=', searchText: '=' }, replace: true, template: '<div ng-hide="searchFor">' + '<h4 ng-show="searchResults">Foram encontrado(s) {{searchResults.length}} resultado(s) com o texto: "{{searchText}}"</h4>' + '<ul ng-show="searchResults">' + '<li ng-repeat="searchResult in searchResults | filter:x">' + '{{searchResult}}' + '</li>' + '</ul>' + '</div>' }; }).filter('checkSource',function(){ return function(items,searchText) { var filtered = []; for (var i = 0; i < items.length; i++) { if (items[i].indexOf(searchText) != -1){ filtered.push(items[i]); } } return filtered; }; }); 3 – Arquivo UserController.js – js/UserController.js function UserController($scope, $timeout, $filter) { $scope.$watch("userSearchText", function(userSearchText) { if(userSearchText) { $scope.userSearchSource = ['Douglas','Jean','Klederson','Rodrigo','Taller']; $scope.userSearchResults = []; $scope.searchingResult = true; $timeout(function() { $scope.searchingResult = false; $scope.userSearchResults = $filter('checkSource') ($scope.userSearchSource,userSearchText); }, 1000); } else { $scope.searchingResult = false; $scope.userSearchResults = []; } }); } Bom é isso ae... até aproxima abraços ;)