SlideShare a Scribd company logo
Cleaner Opinionated AngularJS
Prabhakar Doraisamy	

August 2014
var app = angular.module('app', []);	

app.controller('MyCtrl', function () {

…

});
function MainCtrl () {

	

 …

}	

angular

.module('app', [])

.controller('MainCtrl', MainCtrl);
Better
(function () {

	

 angular.module('app', []);	

	

 function MainCtrl () {

	

 }



	

 angular

	

 .module('app')

	

 .controller('MainCtrl', MainCtrl);

})();
BestDefining a module
• aids in stack traces as functions aren't anonymous • avoids polluting the global namespace
Better
Better #2Dependency injection
• Using the array syntax to declare dependencies works with minification
function MainCtrl(anyVariableName, $q) {	

}

MainCtrl.$inject = ['$http',‘$q’];	

angular

.module('app', [])

.controller('MainCtrl', MainCtrl);
var app = angular.module('app', []);	

app.controller('MainCtrl', [ '$http', '$q', function($http, $q){

…

});
var app = angular.module('app', []);	

app.controller('MainCtrl', function ($http, $q) {

…

});
<div ng-controller="MainCtrl">	

{{ someObject }}	

</div>
<div ng-controller="MainCtrl as main">	

{{ main.someObject }}	

</div>
Better
<div>

{{ main.someObject }}

</div>	

!
function config ($routeProvider) {

$routeProvider

.when('/', {

templateUrl: 'views/main.html',

controller: 'MainCtrl',

controllerAs: 'main'

});

}	

angular

.module('app')

.config(config);
BestAdding controller to a view
• Controllers are classes	

• aids in nested scoping and controller instance reference.
• avoids tight coupling ie., reusable view with different controllers	

• avoids using $parent to access any parent controllers from a child controller
function MainCtrl ($scope) {

$scope.someObject = {};

$scope.doSomething = function () {

};

}	

angular

.module('app')

.controller('MainCtrl', MainCtrl);
function MainCtrl ($scope) {

this.someObject = {};

this._$scope = $scope;

}	

MainCtrl.prototype.doSomething = function () {

// use this._$scope

};	

angular

.module('app')

.controller('MainCtrl', MainCtrl);
Slightly better
function MainCtrl () {

this.someObject = {};

this.doSomething = function () {

};

}	

angular

.module('app')

.controller('MainCtrl', MainCtrl);	

function config ($routeProvider) {

$routeProvider

.when('/', {

templateUrl: 'views/main.html',

controller: 'MainCtrl',

controllerAs: 'main'

});

}
Betterthis and $scope
• Good for inheritance	

• controllerAs syntax uses this keyword inside controllers instead of $scope. 	

• When using controllerAs, the controller is infact bound to $scope, but there is a degree of separation.
function SomeService () {

}	

angular

.module('app')

.factory('$$SomeService', SomeService);
• $ incites they’re public and can be used.	

• $scope and $rootScope	

• $$ are considered private methods.	

• $$listeners, which are available on the Object	

function SomeService () {

}	

angular

.module('app')

.factory('SomeService', SomeService);
Better
• Avoid using two different names for the Service definition, and the function name for consistency
and stack tracing.
Naming services/directives/providers/factories
<input ng-model=“myModel">	

function MainCtrl(){

$scope.$watch('myModel', function () {

// go

});

}
<input ng-model="myModel" ng-change="callback">	

function MainCtrl(){

$scope.callback = function () {

// go

};

} 	

Better
• Avoid $scope.$watch unless there are no others options	

• less performant than binding an expression to something like ng-change
Watching data change
function MainCtrl () {

this.doSomething = function () {

};

}	

angular

.module('app')

.controller('MainCtrl', MainCtrl);
function MainCtrl (SomeService) {

this.doSomething = SomeService.doSomething;

}	

angular

.module('app')

.controller('MainCtrl', MainCtrl);
Better
• delegating logic to Factories/Services	

• maximises reusability
Controller logic
function AnotherService () {

var someValue = '';

var someMethod = function () {

};	

return {

someValue: someValue,

someMethod: someMethod

};

}	

angular

.module('app')

.factory('AnotherService',AnotherService);
function AnotherService () {

var AnotherService = {};

AnotherService.someValue = ‘’;

AnotherService.someMethod = function () {

};	

return AnotherService;

}	

angular

.module('app')

.factory('AnotherService',AnotherService);
Better
• makes internal module namespacing a little easier	

• easy to read any private methods and variables
Factory
function MainCtrl (SomeService) {

this.makeActive = function (elem) {

$(elem).addClass(‘test’);

};

}	

angular

.module('app')

.controller('MainCtrl', MainCtrl);	

function SomeDirective (SomeService) {

return {

restrict: 'EA',

template: [

'<a href="" class="myawesomebutton" ng-transclude>',

'<i class="icon-ok-sign"></i>',

'</a>'

].join(''),	

link: function ($scope, $element, $attrs) {

// DOM manipulation/events here!

$element.on('click', function () {

$(this).addClass('test');

});

}

};

}	

angular

.module('app')

.directive('SomeDirective', SomeDirective);
Better
• Any DOM manipulation should take place inside a directive, and only directives
DOM manipulation
If you are writing $(element) somewhere in your
controller, its a indication that you need a directive.
function ngFocus (SomeService) {

return {};

}	

angular

.module('app')

.directive('ngFocus', ngFocus);
function focusFire (SomeService) {

return {};

}	

angular

.module('app')

.directive('focusFire', focusFire);
Better
• Custom directives should not be ng-* prefixed to prevent future core overrides
Naming a directive
<!-- directive: my-directive -->	

<div class="my-directive"></div>
Followed by adding restrict usage using the restrict property inside each directive's Object. 	

• E for element	

• A for attribute	

• M for comment (avoid)	

• C for className (avoid this too as it's even more confusing, but plays better with IE).	

• Multiple restrictions, such as restrict: 'EA'.
BetterDirective usage restriction
<my-directive></my-directive>	

<div my-directive></div>
function MainCtrl (SomeService) {

var self = this; // unresolved

self.something;

// resolved asynchronously 

SomeService.doSomething().then(function (response) {

self.something = response;

});

}	

angular

.module('app')

.controller('MainCtrl', MainCtrl);
function config ($routeProvider) {

$routeProvider

.when('/', {

templateUrl: 'views/main.html',

resolve: {

doSomething: function (SomeService) {

return SomeService.doSomething();

}

}

});

}

function MainCtrl (SomeService) {

// resolved!

this.something = SomeService.something;

}

angular..module(‘app').controller('MainCtrl', MainCtrl);

angular.module(‘app').config(config);
Better
// config with resolve pointing to relevant controller

function config ($routeProvider) {

$routeProvider

.when('/', {

templateUrl: 'views/main.html',

controller: 'MainCtrl',

controllerAs: 'main',

resolve: MainCtrl.resolve

});

}	

// controller as usual

function MainCtrl (SomeService) {

// resolved!

this.something = SomeService.something;

}	

// create the resolved property

MainCtrl.resolve = {

doSomething: function (SomeService) {

return SomeService.doSomething();

}

};	

angular

.module(‘app')

.controller('MainCtrl', MainCtrl)

.config(config);
BestInit data for a view
• keeping router tidy	

• progress. indicator using the $routeChangeStart event
• keeping controller tidy

More Related Content

What's hot (20)

AngularJS Internal
AngularJS InternalAngularJS Internal
AngularJS Internal
Eyal Vardi
 
AngularJS Basics with Example
AngularJS Basics with ExampleAngularJS Basics with Example
AngularJS Basics with Example
Sergey Bolshchikov
 
Building Robust jQuery Plugins
Building Robust jQuery PluginsBuilding Robust jQuery Plugins
Building Robust jQuery Plugins
Jörn Zaefferer
 
Angular In Depth
Angular In DepthAngular In Depth
Angular In Depth
Mindfire Solutions
 
Ultimate Introduction To AngularJS
Ultimate Introduction To AngularJSUltimate Introduction To AngularJS
Ultimate Introduction To AngularJS
Jacopo Nardiello
 
AngularJS $Provide Service
AngularJS $Provide ServiceAngularJS $Provide Service
AngularJS $Provide Service
Eyal Vardi
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
Eyal Vardi
 
Angular custom directives
Angular custom directivesAngular custom directives
Angular custom directives
Alexe Bogdan
 
AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS Architecture
Eyal Vardi
 
Basics of AngularJS
Basics of AngularJSBasics of AngularJS
Basics of AngularJS
Filip Janevski
 
AngularJS Framework
AngularJS FrameworkAngularJS Framework
AngularJS Framework
Barcamp Saigon
 
Solid angular
Solid angularSolid angular
Solid angular
Nir Kaufman
 
AngularJS.part1
AngularJS.part1AngularJS.part1
AngularJS.part1
Andrey Kolodnitsky
 
AngularJS Basic Training
AngularJS Basic TrainingAngularJS Basic Training
AngularJS Basic Training
Cornel Stefanache
 
ChtiJUG - Introduction à Angular2
ChtiJUG - Introduction à Angular2ChtiJUG - Introduction à Angular2
ChtiJUG - Introduction à Angular2
Demey Emmanuel
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
Wei Ru
 
Technozaure - Angular2
Technozaure - Angular2Technozaure - Angular2
Technozaure - Angular2
Demey Emmanuel
 
AngularJS Animations
AngularJS AnimationsAngularJS Animations
AngularJS Animations
Eyal Vardi
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
Nir Kaufman
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xUpgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.x
Eyal Vardi
 
AngularJS Internal
AngularJS InternalAngularJS Internal
AngularJS Internal
Eyal Vardi
 
Building Robust jQuery Plugins
Building Robust jQuery PluginsBuilding Robust jQuery Plugins
Building Robust jQuery Plugins
Jörn Zaefferer
 
Ultimate Introduction To AngularJS
Ultimate Introduction To AngularJSUltimate Introduction To AngularJS
Ultimate Introduction To AngularJS
Jacopo Nardiello
 
AngularJS $Provide Service
AngularJS $Provide ServiceAngularJS $Provide Service
AngularJS $Provide Service
Eyal Vardi
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
Eyal Vardi
 
Angular custom directives
Angular custom directivesAngular custom directives
Angular custom directives
Alexe Bogdan
 
AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS Architecture
Eyal Vardi
 
ChtiJUG - Introduction à Angular2
ChtiJUG - Introduction à Angular2ChtiJUG - Introduction à Angular2
ChtiJUG - Introduction à Angular2
Demey Emmanuel
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
Wei Ru
 
Technozaure - Angular2
Technozaure - Angular2Technozaure - Angular2
Technozaure - Angular2
Demey Emmanuel
 
AngularJS Animations
AngularJS AnimationsAngularJS Animations
AngularJS Animations
Eyal Vardi
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
Nir Kaufman
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xUpgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.x
Eyal Vardi
 

Similar to Opinionated AngularJS (20)

Coffee@DBG - Exploring Angular JS
Coffee@DBG - Exploring Angular JSCoffee@DBG - Exploring Angular JS
Coffee@DBG - Exploring Angular JS
Deepu S Nath
 
Angular js
Angular jsAngular js
Angular js
Behind D Walls
 
CFUGbe talk about Angular JS
CFUGbe talk about Angular JSCFUGbe talk about Angular JS
CFUGbe talk about Angular JS
Alwyn Wymeersch
 
Angular.js Primer in Aalto University
Angular.js Primer in Aalto UniversityAngular.js Primer in Aalto University
Angular.js Primer in Aalto University
SC5.io
 
Mini-Training: AngularJS
Mini-Training: AngularJSMini-Training: AngularJS
Mini-Training: AngularJS
Betclic Everest Group Tech Team
 
Top 10 Mistakes AngularJS Developers Make
Top 10 Mistakes AngularJS Developers MakeTop 10 Mistakes AngularJS Developers Make
Top 10 Mistakes AngularJS Developers Make
Mark Meyer
 
AngularJS
AngularJSAngularJS
AngularJS
LearningTech
 
Front end development with Angular JS
Front end development with Angular JSFront end development with Angular JS
Front end development with Angular JS
Bipin
 
Angular js-crash-course
Angular js-crash-courseAngular js-crash-course
Angular js-crash-course
Keith Bloomfield
 
Angular1x and Angular 2 for Beginners
Angular1x and Angular 2 for BeginnersAngular1x and Angular 2 for Beginners
Angular1x and Angular 2 for Beginners
Oswald Campesato
 
Angular js
Angular jsAngular js
Angular js
Baldeep Sohal
 
Workshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte IIWorkshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte II
Visual Engineering
 
Building an End-to-End AngularJS Application
Building an End-to-End AngularJS ApplicationBuilding an End-to-End AngularJS Application
Building an End-to-End AngularJS Application
Dan Wahlin
 
AngularJs
AngularJsAngularJs
AngularJs
Hossein Baghayi
 
Modules and injector
Modules and injectorModules and injector
Modules and injector
Eyal Vardi
 
MEAN - Notes from the field (Full-Stack Development with Javascript)
MEAN - Notes from the field (Full-Stack Development with Javascript)MEAN - Notes from the field (Full-Stack Development with Javascript)
MEAN - Notes from the field (Full-Stack Development with Javascript)
Chris Clarke
 
AngularJS Unit Testing
AngularJS Unit TestingAngularJS Unit Testing
AngularJS Unit Testing
Prince Norin
 
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
Learnimtactics
 
AngularJS Custom Directives
AngularJS Custom DirectivesAngularJS Custom Directives
AngularJS Custom Directives
yprodev
 
Angular
AngularAngular
Angular
LearningTech
 
Coffee@DBG - Exploring Angular JS
Coffee@DBG - Exploring Angular JSCoffee@DBG - Exploring Angular JS
Coffee@DBG - Exploring Angular JS
Deepu S Nath
 
CFUGbe talk about Angular JS
CFUGbe talk about Angular JSCFUGbe talk about Angular JS
CFUGbe talk about Angular JS
Alwyn Wymeersch
 
Angular.js Primer in Aalto University
Angular.js Primer in Aalto UniversityAngular.js Primer in Aalto University
Angular.js Primer in Aalto University
SC5.io
 
Top 10 Mistakes AngularJS Developers Make
Top 10 Mistakes AngularJS Developers MakeTop 10 Mistakes AngularJS Developers Make
Top 10 Mistakes AngularJS Developers Make
Mark Meyer
 
Front end development with Angular JS
Front end development with Angular JSFront end development with Angular JS
Front end development with Angular JS
Bipin
 
Angular1x and Angular 2 for Beginners
Angular1x and Angular 2 for BeginnersAngular1x and Angular 2 for Beginners
Angular1x and Angular 2 for Beginners
Oswald Campesato
 
Workshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte IIWorkshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte II
Visual Engineering
 
Building an End-to-End AngularJS Application
Building an End-to-End AngularJS ApplicationBuilding an End-to-End AngularJS Application
Building an End-to-End AngularJS Application
Dan Wahlin
 
Modules and injector
Modules and injectorModules and injector
Modules and injector
Eyal Vardi
 
MEAN - Notes from the field (Full-Stack Development with Javascript)
MEAN - Notes from the field (Full-Stack Development with Javascript)MEAN - Notes from the field (Full-Stack Development with Javascript)
MEAN - Notes from the field (Full-Stack Development with Javascript)
Chris Clarke
 
AngularJS Unit Testing
AngularJS Unit TestingAngularJS Unit Testing
AngularJS Unit Testing
Prince Norin
 
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
Learnimtactics
 
AngularJS Custom Directives
AngularJS Custom DirectivesAngularJS Custom Directives
AngularJS Custom Directives
yprodev
 
Ad

Recently uploaded (20)

Characterization of Polymeric Materials by Thermal Analysis, Spectroscopy an...
Characterization of Polymeric Materials by Thermal Analysis,  Spectroscopy an...Characterization of Polymeric Materials by Thermal Analysis,  Spectroscopy an...
Characterization of Polymeric Materials by Thermal Analysis, Spectroscopy an...
1SI20ME092ShivayogiB
 
"The Enigmas of the Riemann Hypothesis" by Julio Chai
"The Enigmas of the Riemann Hypothesis" by Julio Chai"The Enigmas of the Riemann Hypothesis" by Julio Chai
"The Enigmas of the Riemann Hypothesis" by Julio Chai
Julio Chai
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
sebastianku31
 
Structural Health and Factors affecting.pptx
Structural Health and Factors affecting.pptxStructural Health and Factors affecting.pptx
Structural Health and Factors affecting.pptx
gunjalsachin
 
IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...
ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...
ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...
Journal of Soft Computing in Civil Engineering
 
Influence line diagram in a robust model
Influence line diagram in a robust modelInfluence line diagram in a robust model
Influence line diagram in a robust model
ParthaSengupta26
 
FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.
maldonadocesarmanuel
 
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.pptfy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
sukarnoamin
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
New Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docxNew Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docx
misheetasah
 
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
ijccmsjournal
 
Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)
elelijjournal653
 
Environmental Engineering Wastewater.pptx
Environmental Engineering Wastewater.pptxEnvironmental Engineering Wastewater.pptx
Environmental Engineering Wastewater.pptx
SheerazAhmed77
 
Software Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha TasnuvaSoftware Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha Tasnuva
tanishatasnuva76
 
cloud Lecture_2025 cloud architecture.ppt
cloud Lecture_2025 cloud architecture.pptcloud Lecture_2025 cloud architecture.ppt
cloud Lecture_2025 cloud architecture.ppt
viratkohli82222
 
Influence line diagram for truss in a robust
Influence line diagram for truss in a robustInfluence line diagram for truss in a robust
Influence line diagram for truss in a robust
ParthaSengupta26
 
Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.
Sowndarya6
 
Webinar On Steel Melting IIF of steel for rdso
Webinar  On Steel  Melting IIF of steel for rdsoWebinar  On Steel  Melting IIF of steel for rdso
Webinar On Steel Melting IIF of steel for rdso
KapilParyani3
 
Characterization of Polymeric Materials by Thermal Analysis, Spectroscopy an...
Characterization of Polymeric Materials by Thermal Analysis,  Spectroscopy an...Characterization of Polymeric Materials by Thermal Analysis,  Spectroscopy an...
Characterization of Polymeric Materials by Thermal Analysis, Spectroscopy an...
1SI20ME092ShivayogiB
 
"The Enigmas of the Riemann Hypothesis" by Julio Chai
"The Enigmas of the Riemann Hypothesis" by Julio Chai"The Enigmas of the Riemann Hypothesis" by Julio Chai
"The Enigmas of the Riemann Hypothesis" by Julio Chai
Julio Chai
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
sebastianku31
 
Structural Health and Factors affecting.pptx
Structural Health and Factors affecting.pptxStructural Health and Factors affecting.pptx
Structural Health and Factors affecting.pptx
gunjalsachin
 
IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
Influence line diagram in a robust model
Influence line diagram in a robust modelInfluence line diagram in a robust model
Influence line diagram in a robust model
ParthaSengupta26
 
FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.
maldonadocesarmanuel
 
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.pptfy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
sukarnoamin
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
New Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docxNew Microsoft Office Word Documentfrf.docx
New Microsoft Office Word Documentfrf.docx
misheetasah
 
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
PREDICTION OF ROOM TEMPERATURE SIDEEFFECT DUE TOFAST DEMAND RESPONSEFOR BUILD...
ijccmsjournal
 
Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)
elelijjournal653
 
Environmental Engineering Wastewater.pptx
Environmental Engineering Wastewater.pptxEnvironmental Engineering Wastewater.pptx
Environmental Engineering Wastewater.pptx
SheerazAhmed77
 
Software Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha TasnuvaSoftware Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha Tasnuva
tanishatasnuva76
 
cloud Lecture_2025 cloud architecture.ppt
cloud Lecture_2025 cloud architecture.pptcloud Lecture_2025 cloud architecture.ppt
cloud Lecture_2025 cloud architecture.ppt
viratkohli82222
 
Influence line diagram for truss in a robust
Influence line diagram for truss in a robustInfluence line diagram for truss in a robust
Influence line diagram for truss in a robust
ParthaSengupta26
 
Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.Third Review PPT that consists of the project d etails like abstract.
Third Review PPT that consists of the project d etails like abstract.
Sowndarya6
 
Webinar On Steel Melting IIF of steel for rdso
Webinar  On Steel  Melting IIF of steel for rdsoWebinar  On Steel  Melting IIF of steel for rdso
Webinar On Steel Melting IIF of steel for rdso
KapilParyani3
 
Ad

Opinionated AngularJS

  • 2. var app = angular.module('app', []); app.controller('MyCtrl', function () {
 …
 }); function MainCtrl () {
 …
 } angular
 .module('app', [])
 .controller('MainCtrl', MainCtrl); Better (function () {
 angular.module('app', []); function MainCtrl () {
 }
 
 angular
 .module('app')
 .controller('MainCtrl', MainCtrl);
 })(); BestDefining a module • aids in stack traces as functions aren't anonymous • avoids polluting the global namespace
  • 3. Better Better #2Dependency injection • Using the array syntax to declare dependencies works with minification function MainCtrl(anyVariableName, $q) { }
 MainCtrl.$inject = ['$http',‘$q’]; angular
 .module('app', [])
 .controller('MainCtrl', MainCtrl); var app = angular.module('app', []); app.controller('MainCtrl', [ '$http', '$q', function($http, $q){
 …
 }); var app = angular.module('app', []); app.controller('MainCtrl', function ($http, $q) {
 …
 });
  • 4. <div ng-controller="MainCtrl"> {{ someObject }} </div> <div ng-controller="MainCtrl as main"> {{ main.someObject }} </div> Better <div>
 {{ main.someObject }}
 </div> ! function config ($routeProvider) {
 $routeProvider
 .when('/', {
 templateUrl: 'views/main.html',
 controller: 'MainCtrl',
 controllerAs: 'main'
 });
 } angular
 .module('app')
 .config(config); BestAdding controller to a view • Controllers are classes • aids in nested scoping and controller instance reference. • avoids tight coupling ie., reusable view with different controllers • avoids using $parent to access any parent controllers from a child controller
  • 5. function MainCtrl ($scope) {
 $scope.someObject = {};
 $scope.doSomething = function () {
 };
 } angular
 .module('app')
 .controller('MainCtrl', MainCtrl); function MainCtrl ($scope) {
 this.someObject = {};
 this._$scope = $scope;
 } MainCtrl.prototype.doSomething = function () {
 // use this._$scope
 }; angular
 .module('app')
 .controller('MainCtrl', MainCtrl); Slightly better function MainCtrl () {
 this.someObject = {};
 this.doSomething = function () {
 };
 } angular
 .module('app')
 .controller('MainCtrl', MainCtrl); function config ($routeProvider) {
 $routeProvider
 .when('/', {
 templateUrl: 'views/main.html',
 controller: 'MainCtrl',
 controllerAs: 'main'
 });
 } Betterthis and $scope • Good for inheritance • controllerAs syntax uses this keyword inside controllers instead of $scope. • When using controllerAs, the controller is infact bound to $scope, but there is a degree of separation.
  • 6. function SomeService () {
 } angular
 .module('app')
 .factory('$$SomeService', SomeService); • $ incites they’re public and can be used. • $scope and $rootScope • $$ are considered private methods. • $$listeners, which are available on the Object function SomeService () {
 } angular
 .module('app')
 .factory('SomeService', SomeService); Better • Avoid using two different names for the Service definition, and the function name for consistency and stack tracing. Naming services/directives/providers/factories
  • 7. <input ng-model=“myModel"> function MainCtrl(){
 $scope.$watch('myModel', function () {
 // go
 });
 } <input ng-model="myModel" ng-change="callback"> function MainCtrl(){
 $scope.callback = function () {
 // go
 };
 } Better • Avoid $scope.$watch unless there are no others options • less performant than binding an expression to something like ng-change Watching data change
  • 8. function MainCtrl () {
 this.doSomething = function () {
 };
 } angular
 .module('app')
 .controller('MainCtrl', MainCtrl); function MainCtrl (SomeService) {
 this.doSomething = SomeService.doSomething;
 } angular
 .module('app')
 .controller('MainCtrl', MainCtrl); Better • delegating logic to Factories/Services • maximises reusability Controller logic
  • 9. function AnotherService () {
 var someValue = '';
 var someMethod = function () {
 }; return {
 someValue: someValue,
 someMethod: someMethod
 };
 } angular
 .module('app')
 .factory('AnotherService',AnotherService); function AnotherService () {
 var AnotherService = {};
 AnotherService.someValue = ‘’;
 AnotherService.someMethod = function () {
 }; return AnotherService;
 } angular
 .module('app')
 .factory('AnotherService',AnotherService); Better • makes internal module namespacing a little easier • easy to read any private methods and variables Factory
  • 10. function MainCtrl (SomeService) {
 this.makeActive = function (elem) {
 $(elem).addClass(‘test’);
 };
 } angular
 .module('app')
 .controller('MainCtrl', MainCtrl); function SomeDirective (SomeService) {
 return {
 restrict: 'EA',
 template: [
 '<a href="" class="myawesomebutton" ng-transclude>',
 '<i class="icon-ok-sign"></i>',
 '</a>'
 ].join(''), link: function ($scope, $element, $attrs) {
 // DOM manipulation/events here!
 $element.on('click', function () {
 $(this).addClass('test');
 });
 }
 };
 } angular
 .module('app')
 .directive('SomeDirective', SomeDirective); Better • Any DOM manipulation should take place inside a directive, and only directives DOM manipulation
  • 11. If you are writing $(element) somewhere in your controller, its a indication that you need a directive.
  • 12. function ngFocus (SomeService) {
 return {};
 } angular
 .module('app')
 .directive('ngFocus', ngFocus); function focusFire (SomeService) {
 return {};
 } angular
 .module('app')
 .directive('focusFire', focusFire); Better • Custom directives should not be ng-* prefixed to prevent future core overrides Naming a directive
  • 13. <!-- directive: my-directive --> <div class="my-directive"></div> Followed by adding restrict usage using the restrict property inside each directive's Object. • E for element • A for attribute • M for comment (avoid) • C for className (avoid this too as it's even more confusing, but plays better with IE). • Multiple restrictions, such as restrict: 'EA'. BetterDirective usage restriction <my-directive></my-directive> <div my-directive></div>
  • 14. function MainCtrl (SomeService) {
 var self = this; // unresolved
 self.something;
 // resolved asynchronously 
 SomeService.doSomething().then(function (response) {
 self.something = response;
 });
 } angular
 .module('app')
 .controller('MainCtrl', MainCtrl); function config ($routeProvider) {
 $routeProvider
 .when('/', {
 templateUrl: 'views/main.html',
 resolve: {
 doSomething: function (SomeService) {
 return SomeService.doSomething();
 }
 }
 });
 }
 function MainCtrl (SomeService) {
 // resolved!
 this.something = SomeService.something;
 }
 angular..module(‘app').controller('MainCtrl', MainCtrl);
 angular.module(‘app').config(config); Better // config with resolve pointing to relevant controller
 function config ($routeProvider) {
 $routeProvider
 .when('/', {
 templateUrl: 'views/main.html',
 controller: 'MainCtrl',
 controllerAs: 'main',
 resolve: MainCtrl.resolve
 });
 } // controller as usual
 function MainCtrl (SomeService) {
 // resolved!
 this.something = SomeService.something;
 } // create the resolved property
 MainCtrl.resolve = {
 doSomething: function (SomeService) {
 return SomeService.doSomething();
 }
 }; angular
 .module(‘app')
 .controller('MainCtrl', MainCtrl)
 .config(config); BestInit data for a view • keeping router tidy • progress. indicator using the $routeChangeStart event • keeping controller tidy