Showing posts with label AngularJS. Show all posts
Showing posts with label AngularJS. Show all posts

Monday, June 5, 2017

How to connect one model variable to mutiple html elements using angularjs data binding?

@reference_1_stackoverflow
How to connect one model variable to mutiple html elements using angularjs data binding?

@reference_1_toptal
Top 18 Most Common Mistakes that AngularJS Developers Make

Common Mistake #2: Not Having a Dot In There

You probably have read that if you were not having a dot in your ng-model, you were doing it wrong. When it regards inheritance, that statement is often true. Scopes have a prototypal model of inheritance, typical to JavaScript, and nested scopes are common to AngularJS. Many directives create child scopes such as ngRepeat, ngIf, and ngController. When resolving a model, the lookup starts on the current scope and goes through every parent scope, all the way to $rootScope.
But, when setting a new value, what happens depends on what kind of model (variable) we want to change. If the model is a primitive, the child scope will just create a new model. But if the change is to a property of a model object, the lookup on parent scopes will find the referenced object and change its actual property. A new model would not be set on the current scope, so no masking would occur:
function MainController($scope) {
  $scope.foo = 1;
  $scope.bar = {innerProperty: 2};
}

angular.module(‘myApp’, [])
.controller('MainController', MainController);
<div ng-controller=“MainController">
  <p>OUTER SCOPE:</p>
  <p>{{ foo }}</p>
  <p>{{ bar.innerProperty }}</p>
  <div ng-if=“foo”> <!— ng-if creates a new scope —>
    <p>INNER SCOPE</p>
    <p>{{ foo }}</p>
    <p>{{ bar.innerProperty }}</p>
    <button ng-click="foo = 2”>Set primitive</button>
    <button ng-click=“bar.innerProperty = 3”>Mutate object</button>
  </div>
</div>
Clicking the button labelled “Set primitive” will set foo in the inner scope to 2, but will not change foo in the outer scope.
Clicking the button labelled “Change object” will change the bar property from the parent scope. Since there is no variable on the inner scope, no shadowing will happen, and the visible value for bar will be 3 in both scopes.
Another way to do this is to leverage the fact that the parent scopes and the root Scope are referenced from every scope. The $parent and $root objects can be used to access the parent scope and $rootScope, respectively, directly from the view. It may be a powerful way, but I am not a fan of it due to the problem with targeting a particular scope up the stream. There is another way to set and access properties specific to a scope - using the controllerAs syntax.


@reference_3_itfashion4u
 AnularJS 'limitTo' filter inside/outside div element with 'ng-if' attribute

AnularJS 'limitTo' filter inside/outside div element with 'ng-if' attribute

The 'ng-repeat' limitTo filter won't work unless you remove the ng-if='true' attribute or put the <ul> list inside div element.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body ng-app="myApp" ng-controller="namesCtrl">

<div ng-if='true'>

<p>Type a letter in the input field:</p>

<p><input type="number" ng-model="test"></p>
</div>
<ul>
  <li ng-repeat="x in names | limitTo:test">
    {{ x }}
  </li>
</ul>

<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [
        'Jani',
        'Carl',
        'Margareth',
        'Hege',
        'Joe',
        'Gustav',
        'Birgit',
        'Mary',
        'Kai'
    ];
});
</script>

<p>The list will only consists of names matching the filter.</p>
</body>
</html>

@reference_1_w3schools
@reference_2_youtube

@reference_3_itfashion4u
How to connect one model variable to mutiple html elements using angularjs data binding?

Sunday, June 4, 2017

Showing Spinner GIF during $http request in angular

@reference_1_stackoverflow
Showing Spinner GIF during $http request in angular

@reference_2_stackoverflow
Delay loading data in Angular JS

Use this 'ng-cloak' to avoid the undesirable flicker effect caused by the html template display

What you are looking for is ng-cloak.
You have to add it like this:
<body class="{{ bodyClass }}" ng-cloak> 
Edit:
It is also advisable to put the snippet below into your CSS file, according to docs.
"For the best result, the angular.js script must be loaded in the head section of the html document; alternatively, the css rule above must be included in the external stylesheet of the application."
[ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak { display: none !important; }


@reference_1_stackoverflow
AngularJS strategy to prevent flash-of-unstyled-content for a class

@reference_2_angularjs.org
ngCloak - directive in module ng
 
* @reference_3_stackoverflow  
 AngularJS: How to prevent “code flash” in page while loading
 
 

Tuesday, May 23, 2017

Create a global filter injecting '$sce.trustAsHtml' to bind unsafe HTML

var parking = angular.module("parking", []);
parking.controller("parkingCtrl", function ($scope) {
    $scope.appTitle = "<b>[Packt] Parking</b>";
});

parking.filter('safeHtml', function ($sce) {
    return function (val) {
        return $sce.trustAsHtml(val);
    };
});
<html ng-app="parking">
<head>
<title>[Packt] Parking</title>
<script src="https://....../angular.js/1.6.1/angular.js"></script>
<script src="//.....org/1.2.20/angular-sanitize.min.js"></script>
</head>
<body ng-controller="parkingCtrl">
<h3 ng-bind-html="appTitle | safeHtml"></h3>
</body>
</html>
 
@reference_1_stackoverflow
@reference_2_docs.angularjs

Sunday, May 21, 2017

AngularJS: Insert HTML from a string || render HTML within double curly brace notation

Do you want to show the HTML (like <b>Hello</b>) or render the HTML (like Hello) ?
  • If you want to show it, the curly bracket is enough. However if the html you have has html entities (like &lt;stuff) you need to manually unescape it, see this SO question.
  • If you want to render it, you need to use the ng-bind-html directive instead of curcly bracket (which, FYI, is a shortcut to the ng-bind directive). You need to tell Angular that the content injected into that directive is safe, using $sce.trustAsHtml.
See example of both cases below:

angular.module('test', []).controller('ctrl', function($scope, $sce) {
  $scope.HTML = '<b>Hello</b>';
  
  $scope.trust = $sce.trustAsHtml;
});
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="test" ng-controller="ctrl">
  <div>Show: {{HTML}}</div>
  <div>Render: <span ng-bind-html="trust(HTML)"></span></div>
</div>
 
@reference_1_stackoverflow 

Saturday, May 20, 2017

Dependency Injection & Create custom service in AngularJS

angular.module('myModule', [])
.factory('serviceId', ['depService', function(depService) {
  // ...
}])
.directive('directiveName', ['depService', function(depService) {
  // ...
}])
.filter('filterName', ['depService', function(depService) {
  // ...
}]); 
@reference_1_angularjs.org  
app.factory('<service>', ['$http', function(http) {
  var shinyNewServiceInstance;
  return shinyNewServiceInstance;
}]); 
@reference_2_stackoverflow  
Create custom service in AngularJS 

send POST in angularjs with multiple params

@reference_1_stackoverflow
@reference_2_angularjs.org
@reference_3_stackoverflow

Saturday, May 13, 2017

AngularJS Tutorials

@reference_1_youtube
@reference_2_w3schools
@reference_3_youtube
@reference_4_angularjs.org

“shim” for AngularJS (& jQuery ) when using Require.js

The rule is pretty simple: if a library/script/package/plugin is AMD-aware, then you don't need a shim. (Actually, you must not use a shim for it.) If it is not AMD-aware, then you need a shim.
A library/etc is AMD-aware if it detects that an AMD loader is present and calls define to make itself known to the loader.
jQuery from about 1.8 onwards has not needed a shim because it calls define. Angular, on the other hand, does not call define.
To know whether a specific piece of code needs a shim, you can read its documentation or if the documentation is not clear on this, then you can check the source code for a call to define. For instance jQuery 1.11.0 has this code:
// Register as a named AMD module, since jQuery can be concatenated with other
// files that may use define, but not via a proper concatenation script that
// understands anonymous AMD modules. A named AMD is safest and most robust
// way to register. Lowercase jquery is used because AMD module names are
// derived from file names, and jQuery is normally delivered in a lowercase
// file name. Do this after creating the global so that if an AMD module wants
// to call noConflict to hide this version of jQuery, it will work.
if ( typeof define === "function" && define.amd ) {
    define( "jquery", [], function() {
        return jQuery;
    });
}
How it looks like will vary from one case to the other but the basic think you want to look for is the check that define exists, is a function, has the amd property set and the call to define.
(Note that jQuery is a special case where they decided to hard code the name of the module in the define call (first parameter: jquery). Generally the name of the module won't be present in the define call but will be left for RequireJS to infer on the basis of the file name.)

@reference_1_stackoverflow
@reference_2_requirejs.org