(youtube.com/user/khanLearning)
<input ng-model="name">input with ng-model becomes angular directive Setup a keydown listener to input
<h1>{{name}}</h1>Something in angular named: $interpolation interpolation: markup with expression => string interpolation setup a $watch in current scope $watch will be notified if "name" changes $compile vs $interpolate vs $parse
<div ng-show="myValue"></div>
<span ng-if="checked"> This is removed when the checkbox is unchecked. </span>
{{ filter_expression | filter : expression : comparator}}
show filter demo
limit DOM filter{{ filter_expression | filter : expression : comparator }}
$filter('filter')(array, expression, comparator);how often filter executes? Filters are really simple to use, we insert a pipe, the filter name and we’re done. However, Angular runs every single filter twice per $digest cycle once something has changed. This is some pretty heavy lifting. The first run is from the $$watchers detecting any changes, the second run is to see if there are further changes that need updated values. Here’s an example of a DOM filter, these are the slowest type of filter, preprocessing our data would be much faster. If you can, avoid the inline filter syntax.
<li ng-repeat="item in filteredItems()">
<li ng-repeat="item in items">
<table> <tr ng-repeat="d in data | limitTo:totalDisplayed"> <td>{{d}}</td> </tr> </table>debounce user input (searching) tuning long list
<div ng-repeat=”item in array”> I live and die by {{item}}. <div>
<div ng-repeat=”item in array track by item.Id”> I live until {{array.length}} is less than {{$index}}. <div>ng-repeat performance by track by
<p>One time binding {{::name}}</p> <p>Normal binding {{name}}</p>
<div name="attr: {{::color}}">text: {{::name}}</div>a directive with bidirectional binding and the parameters will not change:
<div some-directive name="::myName" color="My color is {{::myColor}}"></div>a directive that takes an expression:
<ul> <li ng-repeat="item in ::items">{{item.name}};</li> </ul>
Each time a Model is updated, either through user input in the View, or via service input to the Controller, Angular runs something called a $digest cycle.
This cycle is an internal execution loop that runs through your entire application’s bindings and checks if any values have changed. If values have changed, Angular will also update any values in the Model to return to a clear internal state.
When we create data-bindings with AngularJS, we’re creating more $$watchers and $scope Objects, which in turn will take longer to process on each $digest.
<input ng-model="name" ng-model-options="{ debounce: 250 }" />show it in demo delay digest cycle
$(elem).myPlugin({ onchange: function (newValue) { // model changes outside of Angular $(this).val(newValue); // tell Angular values have changed and to update via $digest $scope.$apply(); } });code example in the middle
Every time $watch is called on a scope value, or a value is bound from the DOM with interpolation, an ng-repeat, an ng-switch, and ng-if, or any other DOM attribute/element, a function gets added to the $$watchers array of the innermost scope.
$watch(watchExpression, listener, [objectEquality]);
Watching by value (scope.$watch (watchExpression, listener, true)) detects any change in an arbitrarily nested data structure. It is the most powerful change detection strategy, but also the most expensive. A full traversal of the nested data structure is needed on each digest, and a full copy of it needs to be held in memory.
Angular provides the ability to watch entire objects by passing a third, optional true parameter to scope.$watch. This is, for lack of better words, a terrible idea. A much better solution is to rely on services and object references to propagate object changes between scopes.
$watchCollection(obj, listener);
All that time spent invoking change watchers was mostly wasted.
To address this, we created a directive that “hides” the change watchers of its children, allowing them to be invoked only when the value of a specified parent expression changes. With this change, we avoided invoking thousands of per-word change watchers on every mouse click or other minor event.
var unbinder = scope.$watch('scopeValueToBeWatcher', function(newVal, oldVal){ //do something }); unbinder(); //this line removes the watch from $$watchers.check this one
$scope.$watch(‘listOfBigObjects’, myHandler, true);
$scope.$watch(function($scope) { return $scope.listOfBigObjects. map(function(bigObject) { return bigObject.foo.fieldICareAbout; }); }, myHandler, true);
myApp.directive(function() { return { link: function(s, elem, attr) { s.foo = scope.$eval(attr.foo); scope.$watch(attr.list, function(list) { // do something }, true); }}; });
myApp.directive(function($parse) { return { compile: function(elem, attr) { var fooExp = $parse(attr.foo), listExp = $parse(attr.list); return function link(s, elem) { s.foo = fooExp(s); scope.$watchCollection(listExp, function(list) { // do something }); }; } }; });so put as much as possible inside compile
//from angular 1.3 config(['$routeProvider', '$compileProvider', function($routeProvider, $compileProvider) { $compileProvider.debugInfoEnabled(false); }]ng-perf.com
(youtube.com/user/khanLearning)