introduction to angularjs for wordpress developers

23
Introduction To AngularJS For WordPress Developers Josh Pollock

Upload: josh-pollock

Post on 13-Jan-2017

1.676 views

Category:

Internet


8 download

TRANSCRIPT

Page 1: Introduction to AngularJS For WordPress Developers

Introduction To AngularJS For

WordPress Developers

Josh Pollock

Page 2: Introduction to AngularJS For WordPress Developers

@Josh412

Page 3: Introduction to AngularJS For WordPress Developers

JoshPress.net/wordcamp-miami-angularjs

Page 4: Introduction to AngularJS For WordPress Developers

Why Not X?React, Backbone, Ember, Etc...

Page 5: Introduction to AngularJS For WordPress Developers

MVCPart One

Page 6: Introduction to AngularJS For WordPress Developers

MVC● Model● View● Controller

MVC

Page 7: Introduction to AngularJS For WordPress Developers

ModelThe model is the current set of data, defined by the controller,

displayed by the view.

MVC

Page 8: Introduction to AngularJS For WordPress Developers

View● The visual representation of

the data.● In Angular this is an HTML

file.

MVC

Page 9: Introduction to AngularJS For WordPress Developers

Controller● Keeps the models up-to-

date using the remote API.● Updates the model based on

your interactions with the view.

MVC

Page 10: Introduction to AngularJS For WordPress Developers

BindingsPart Two

Page 11: Introduction to AngularJS For WordPress Developers

Bindings● Connects views to

controllers.● HTML5 Attributes● Template Tags: Curly

Brackets

<div ng-controller="postExample">

<form>

<input type="text" ng-model="post.title" />

</form>

<div>{{post.title}}</div>

</div>

Controller

Page 12: Introduction to AngularJS For WordPress Developers

Bindings● Use controller function to

create controller...● $scope is available in view

(function(angular) {

'use strict';

angular.module('learnAngular', [])

.controller('postExample', ['$scope', function

($scope) {

$scope.post = {

title: 'Enter Title'

};

}]);

})(window.angular);

Template

Page 13: Introduction to AngularJS For WordPress Developers

Bindings● Bindings can be used to call

functions● Examples:

○ ng-submit○ ng-hide

<div ng-controller="postExample">

<form ng-submit="submit()">

<input type="text" ng-model="post.title" />

<input type="submit" value="Save" ng-hide="post.title == 'Enter Title'" />

</form>

<div>{{post.title}}</div>

</div>

Views

Page 14: Introduction to AngularJS For WordPress Developers

Bindings● Define functions for view on

$scope.● Example: $scope.submit

(function(angular) {

'use strict';

angular.module('learnAngular', [])

.controller('postExample', ['$scope', function($scope) {

$scope.post = {

title: 'Enter Title'

};

$scope.submit = function(){

alert( 'saving' );

}

}]);

})(window.angular);

Controller

Page 15: Introduction to AngularJS For WordPress Developers

Bindings● ng-repeat:

○ Repeat items (like a list of posts)

<div ng-controller="postsExample">

<h3>Posts:</h3>

<div ng-repeat="post in posts">

{{post.title}}

</div>

</div>

Views

Page 16: Introduction to AngularJS For WordPress Developers

Bindings● Look mom, two controllers!● Iterating over posts array.

(function(angular) {

'use strict';

angular.module('learnAngular', [])

.controller('postExample', ['$scope', function($scope) {

$scope.post = {

title: 'Enter Title'

};

$scope.submit = function(){

alert( 'saving' );

}

}]).controller('postsExample', ['$scope', function($scope) {

$scope.posts = [

{ title: 'Post One' },

{ title: 'Post Two' }

];

}]);

})(window.angular);

Controller

Page 17: Introduction to AngularJS For WordPress Developers

HTTPPart Three

Page 18: Introduction to AngularJS For WordPress Developers

Angular HTTP● Use to connect model to

remote API● Syntax similar to jQuery

AJAX

(function(angular) {

'use strict';

angular.module('learnAngular', [])

.controller('postExample', ['$scope', '$http', function($scope, $http) {

$http({

url: 'http://joshpress.net/wp-json/wp/v2/posts/1',

cache: true

} ).then( function( res ) {

$scope.post = res.data;

});

}]).controller('postsExample', ['$scope', '$http', function($scope, $http) {

$http( {

url: 'http://joshpress.net/wp-json/wp/v2/posts/',

cache: true

} ).then( function ( res ) {

$scope.posts = res.data;

$scope.totalPages = res.headers('x-wp-totalpages');

$scope.total = res.headers( 'x-wp-total' );

} );

}]);

})(window.angular);

Page 19: Introduction to AngularJS For WordPress Developers

Angular HTTP● Supports all methods

○ IE Use POST to save● Caching… promises.. etc.

(function(angular) {

'use strict';

angular.module('learnAngular', [])

.controller('postExample', ['$scope', '$http', function($scope, $http) {

$http({

url: 'http://joshpress.net/wp-json/wp/v2/posts/1',

cache: true

} ).then( function( res ) {

$scope.post = res.data;

});

}]).controller('postsExample', ['$scope', '$http', function($scope, $http) {

$http( {

url: 'http://joshpress.net/wp-json/wp/v2/posts/',

cache: true

} ).then( function ( res ) {

$scope.posts = res.data;

$scope.totalPages = res.headers('x-wp-totalpages');

$scope.total = res.headers( 'x-wp-total' );

} );

}]);

})(window.angular);

Page 20: Introduction to AngularJS For WordPress Developers

Angular HTTP● Use to connect model to

remote API● Syntax similar to jQuery

AJAX

(function(angular) {

'use strict';

angular.module('learnAngular', [])

.controller('postExample', ['$scope', '$http', function($scope, $http) {

$http({

url: 'http://joshpress.net/wp-json/wp/v2/posts/1',

cache: true

} ).then( function( res ) {

$scope.post = res.data;

});

$scope.save = function(){

$http({

url: 'http://joshpress.net/wp-json/wp/v2/posts/1',

cache: true,

method: "POST"

} ).then( function( res ) {

$scope.post = res.data;

});

};

}]);

})(window.angular);

Page 21: Introduction to AngularJS For WordPress Developers

Go FurtherPart Next

Page 23: Introduction to AngularJS For WordPress Developers

CalderaWP.comIngotHQ.com@Josh412

JoshPress.net