As we already know that in Angular JS 2.0 $scope will be not available for further use. So, before we migrate to AngularJS 2.0 from current version, we need to understand how to develop directive or controller files without $scope. In my earlier article, I already discussed how to use page controller without $scope. Now, in this article I am going to discuss how to create directive without $scope. Since the main problem to develop an AngularJS directive without $scope is that we need to pass model value from main parent page to directive and vice versa. For this purpose, we can use bindToController in place of scope object in the directive. It is used for isolated scope. The sample structure for the directive is as in the following:
- scope: {},
- bindToController:
- {
- name: "="
- }
- 'use strict';
- var testApp = angular.module('TestApp', []);
- testApp.directive('sampleDirective', ['$http', function ()
- {
- return
- {
- restrict: "E",
- scope: {},
- bindToController:
- {
- args: '='
- },
- template: '<div class="row">' +
- '<select class="form-control"' +
- 'data-ng-model="model.args.selectedItem"' +
- 'data-ng-options="item[model.args.displayField] for item in model.args.source"' +
- 'data-ng-change="model.itemChange(model.args.selectedItem)">' +
- '<option value="">Select Any Item</option>' +
- '</select>' +
- '</div>',
- controller: function ($http)
- {
- var self = this;
- var initializeControl = function ()
- {
- if (self.args == undefined)
- {
- self.args = {};
- }
- if (self.args.method == undefined)
- {
- self.args.method = {};
- }
- if (self.args.isDisabled == undefined)
- {
- self.args.isDisabled = false;
- }
- if (self.args.displayField == undefined)
- {
- self.args.displayField = '';
- alert('Display Field is blank for dropdown control.')
- }
- if (self.args.valueField == undefined)
- {
- self.args.valueField = '';
- alert('Value Field is blank for dropdown control.')
- }
- if (self.args.source == undefined)
- {
- self.args.source = {};
- }
- if (self.args.hide == undefined)
- {
- self.args.hide = false;
- }
- }
- var assignMethod = function ()
- {
- self.args.method =
- {
- setEnable: function (args) {
- self.args.isDisabled = !args;
- },
- setVisible: function (args)
- {
- self.args.hide = !args;
- },
- getText: function ()
- {
- return self.args.selectedText;
- },
- getValue: function () {
- return self.args.selectedValue;
- }
- }
- }
- self.itemChange = function (item)
- {
- if (item != undefined)
- {
- var index = self.args.source.indexOf(item);
- self.args.selectecText = item[self.args.displayField];
- self.args.selectecValue = item[self.args.valueField];
- self.args.selectedItem = item;
- self.args.selectedIndex = index;
- }
- }
- initializeControl();
- assignMethod();
- },
- controllerAs: 'model'
- }
- }]);
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Without Scope Directive</title>
- <link href="../../RefStyle/bootstrap.min.css" rel="stylesheet" />
- <script src="../../Scripts/angular.min.js"></script>
- <script src="SampleDirective.js"></script>
- <script src="IndexController.js"></script>
- </head>
- <body data-ng-app="TestApp" data-ng-controller="IndexController as model">
- <div class="row animated fadeInRight">
- <div class="col-lg-12">
- <div class="rowDiv">
- <h3>{{model.message}}</h3>
- </div>
- </div>
- <div class="col-md-6">
- <div class="rowDiv">
- <sample-directive args="model.args"></sample-directive>
- </div>
- </div>
- <div class="col-md-3">
- <div class="rowDiv">
- {{model.args.selectecText}}
- </div>
- </div>
- <div class="col-md-3">
- <div class="rowDiv">
- {{model.args.selectecValue}}
- </div>
- </div>
- </div>
- </body>
- </html>
- testApp.controller('IndexController', ['$http', function ($http, urlService)
- {
- var self = this;
- self.message = 'Directive Sample with out Scope';
- var dropData = [{ Id: 1, City: 'Kolkata', StudentName: 'Rajiv' },
- { Id: 2, City: 'New Delhi', StudentName: 'Swapan' },
- { Id: 3, City: 'Mumbai', StudentName: 'Nilesh' }];
- self.args = {
- displayField: 'StudentName',
- valueField: "Id",
- source: dropData,
- selectecText: '',
- selectecValue: ''
- };
- }]);


Sr KarthigaPosted Apr 19, 2016, 10:59 PM
Nice explanation
Former memberPosted Feb 6, 2016, 12:02 AM
Yes, we can remove scope.. But, you made it Isolated Scope and assigned this into variable self. So, it removed the requirement of scope. this is very powerful keyword into JavaScript.
Pankaj Kumar ChoudharyPosted Jan 14, 2016, 5:07 AM
Nice Explain Sir...........
Ankit BansalPosted Jan 14, 2016, 12:13 AM
thanks for sharing..keep it up