On Github victormejia / whats-new-angular13
Created by Victor Mejia
Software Dev @ LoopNet (CoStar Group)
I ♥ JavaScript
Tweeting @victorczm
Coding @victormejia
Blogging @ victormejia.me
option for finding places in your app that won't minify correctly because of the DI syntax
<div ng-app="myApp" ng-strict-di=""> <div ng-controller="AppCtrl">{{message}}</div> </div>
angular.module('app', []) .controller('MyCtrl', function ($scope, $filter) { $scope.message = "hello"; }); // that could be minified to this: what is a? angular.module('app', []).controller('MyCtrl', function (a, b) {a.message = "hello"; });
angular.module('app', []) .controller('AppCtrl', ['$scope', '$filter', function ($scope, $filter) { $scope.message = "hello"; }]);
function AppCtrl($scope, $http, $filter) { // ... } AppCtrl.$inject = ["$scope", "$http", "$filter"]; angular.module("myApp", []) .controller("AppCtrl", AppCtrl)
prefix bindings with "::", then only interpolated once and no longer watched
<!-- view is updated immediately --> <input type="text" ng-model="userName"> Hello, my name is {{userName}}
<!-- view will updated only after the first keystroke --> <input type="text" ng-model="userName"> Hello, my name is {{::userName}}
allows us to control how ngModel updates are done
update model (trigger $digest)...
<!-- will only be updated when user removes focus --> <input type="text" ng-model="searchText" ng-model-options="{ updateOn : 'blur' }"> <!-- default update behavior and on blur --> <input type="text" ng-model="searchText" ng-model-options="{ updateOn : 'default blur' }">
<!-- model update delay of 300ms --> <input type="text" ng-model="searchText" ng-model-options="{ debounce : 300 }"> Search: {{searchText}}
specify an object with either 'default' or 'blur' props
<!-- model update delay of 300ms on user input, and immediately on blur --> <input type="text" ng-model="searchText" ng-model-options="{ debounce: { 'default': 300, 'blur': 0 } }"> Search: {{searchText}}
<div ng-controller="ExampleController"> <form name="userForm"> Name: <input type="text" name="userName" ng-model="user.name" ng-model-options="{ debounce: 1000 }"> <button ng-click="userForm.userName.$rollbackViewValue(); user.name=''">Clear</button><br> </form> <pre>user.name = <span ng-bind="user.name"></span></pre> </div>
var _age = 27; $scope.user = { age: function (newAge) { if (angular.isNumber(newAge) && newAge > 0) { return ($scope.age = newAge); } return $scope.age; } }
<div ng-controller="ExampleController"> <form name="userForm"> Age: <input type="text" name="userAge" ng-model="user.age" ng-model-options="{ getterSetter: true }"> </form> user.age = <span ng-bind="user.age()"></span> </div>
manages the logic of passing values b/n DOM and scope and handles parsing, formatting, validation
<input type="email" ng-model="data.email" required="">
<form name="userForm"> <input type="email" name="email" minlength="8" ng-model="data.email" required=""> </form>
// userForm.email.$error { "email" : true "required": false, }
src: http://www.yearofmoo.com/2014/09/taming-forms-in-angularjs-1-3.html
src: http://www.yearofmoo.com/2014/09/taming-forms-in-angularjs-1-3.html
<form name="userForm"><input type="email" ng-model="data.email" required=""></form> <div ng-if="userForm.$submitted || userForm.email.$touched" ng-messages="userForm.email.$error" class="errors"> <div ng-message="required">You did not enter an email.</div> <div ng-message="required">You did not enter a valid email.</div> </div>
// // <input type="text" custom-validator="" ng-model="myModel"> // app.directive('customValidator', function() { require : 'ngModel', link : function(scope, element, attrs, ngModel) { ngModel.$validators.myValidator = function() { // return true or false } } });
return a promise
ngModule.directive('usernameAvailableValidator', ['$http', function($http) { return { require : 'ngModel', link : function($scope, element, attrs, ngModel) { ngModel.$asyncValidators.usernameAvailable = function(username) { return $http.get('/api/username-exists?u='+ username); }; } } }])
<form name="userForm" ng-submit="submit(userForm.$valid)"> <input type="email" name="email" minlength="8" ng-model="data.email" required=""> </form>
$scope.submit = function (valid) { if (!valid) return; $http.post(...) }
© 2014 PEANUTS Worldwide LLC