Pankaj Parkar
Indian | 30 | Ex MVP | Angular GDE
Pankaj Parkar
(TSS Consultancy PVT LTD.)
6. ng-if vs ng-show.
7. Folder structure.
8. Follow Dependency Injection pattern.
9. Utilization of JQuery.
10. Use ui-router named view feature.
When new prototypically inherited scope gets created inside child scope,
that time primitive type variable copy gets passed down to the child scope & object value reference gets passed. If inner scope changes primitive value it doesn't update the parent value copy. Where reference data value do it correctly.
<div ng-app="app" ng-controller="outerCtrl" class="border">
<p>Outer Controller Scope:</p>
<p>{{ foo }}</p>
<div ng-if="showInnerContent" class="border">
<p>Inner Scope</p>
<p>{{ foo }}</p>
<button ng-click="foo='Something Else'">
Change Foo
</button>
</div>
</div>
Lets define object inside a controller like "$scope.model = {};" and place all property in it, here we're going to put `foo` in that.
<div ng-app="app" ng-controller="outerCtrl" class="border">
<input ng-model="model.foo"/>
<p>Outer Controller Scope:</p>
<p>{{ model.foo }}</p>
<div ng-if="showInnerContent" class="border">
<p>Inner Scope</p>
<p>{{ model.foo }}</p>
<button ng-click="foo='Something Else'">
Change Foo from inner scope
</button>
</div>
</div>
Another correct way to resolve binding issue is use `controllerAs` pattern where scope binding issue solved.
<div ng-app="app" ng-controller="outerCtrl as vm" class="border">
<input ng-model="vm.foo"/>
<p>Outer Controller Scope:</p>
<p>{{ vm.foo }}</p>
<div ng-if="vm.showInnerContent" class="border">
<p>Inner Scope</p>
<p>{{ vm.foo }}</p>
<button ng-click="vm.foo='Something Else'">
Change Foo from inner scope
</button>
</div>
</div>
var app = angular.module('app', []);
app.controller('myCtrl', function($scope) {
//controller data
});
app.service('sharedService', function() {
//service code only.
});
//so other angular comonents like factory, filter, etc.
//would also gets added inside "app" module.
//In large scale application it become difficult to see which code appears to be where
//module for services
angular.module('myApp.services', []);
//module for services
angular.module('myApp.controllers', ['myApp.services']);
//module for services
angular.module('myApp.filters', []);
//module for services
angular.module('myApp.pipes', []);
//module for services
angular.module('myApp.shareService', []);
//can be combine to one our main application
angular.module('app', [
'myApp.services',
'myApp.controllers',
'myApp.pipes',
'myApp.services'
]);
app.controller('myCtrl', function($scope, $element) {
var count = 0;
var interval = $interval(function() {
console.log(++count)
}, 5000)
$scope.$on('$destroy', function(event, data) {
$interval.cance(interval);
});
//since $scope would get remove once you use controllerAs
//when you are using controllerAs pattern, use below thing
$element.on('$destroy', function(event, data) {
$interval.cance(interval);
});
});
$scope.output = 'Result';
var template = '<div>{{output}}</div>';
angular.element(document.getElementById('testDiv')).append(template);
$scope.output = 'Result';
var template = '<div>{{output}}</div>';
var compiledTemplate = $compile(template)($scope);
angular.element(document.getElementById('testDiv')).append(compiledTemplate);
<div ng-show="showGrids">
<pm-kendo-grid></pm-kendo-grid>
<pm-dev-express-grid></pm-dev-express-grid>
<pm-ig-grid></pm-ig-grid>
<div>
First lets see what is dependency injection
There are 3 types of DI Annotation
Here we create an array inside which 1st we inject angular dependency in string & then use the same in function
someModule.controller('MyController', ['$scope', 'myService',
function($scope, myService) {
//Awesome controller code here
}
]);
someModule.service('myService', ['$log',
function($log) {
//Awesome service code here
}
]);
Here we create an array inside which 1st we inject angular dependency in string & then use the same in function
function myController($scope, myService) {
//Awesome controller code here
}
someModule.controller('myController', ['$scope', 'myService', MyController]);
function myService($log) {
//Awesome service code here
}
someModule.service('myService', ['$log', myService]);
Here we create an array inside with dependency & add that on function $inject property.
function myController($scope, myService) {
//Awesome controller code here
}
myController.$inject = ['$scope', 'myService', MyController];
someModule.controller('myController', myController);
function myService($log) {
//Awesome service code here
}
myService.$inject = ['$log'];
someModule.service('myService', myService);
This one of the simplest DI technique to inject dependencies
function myController($scope, myService) {
//Awesome controller code here
}
someModule.controller('myController', myController);
function myService($log) {
//Awesome service code here
}
someModule.service('myService', myService);
Developer do use more simplest thing
function myController($scope, myService) {
//Awesome controller code here
}
someModule.controller('myController', myController);
But what happens is when you minify such files to reduce number of roundtrip to server, this DI technique messed up minification
function myController(a,b){};someModule.controller('myController', myController);
By Pankaj Parkar