I would like to know, when we have directives with an isolated area, is it good to have a method in an isolated area, or should we create a method in the parent area of the directive and pass it as a reference to the directive with "&".
For example, in the following code, I created the doSomething method in a directive isolated area and this directive is used three times (according to the data). So, now does angular create a “doSomething” object 3 times in each isolated area, or will it be only one method shared by three isolated areas?
angular.module("app", []).directive('appDirective', appDirective);
function appDirective() {
var directive = {
restrict: 'EA',
template: '<span>{{obj.name}} <input type="checkbox" ng-model="obj.flag" ng-change="doSmothing(obj.flag)"></span> {{obj}}',
replace: false,
controller: controllerFunction,
scope: {
obj: '=appDirective'
}
};
function controllerFunction($scope) {
$scope.doSmothing = function(flag) {
console.log($scope.obj, flag);
}
}
return directive;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="app">
<body>
<div>
<ul ng-init="data=[{name:'Shiva',flag:false},{name:'Kumar',flag:false},{name:'Govind',flag:false}]">
<li ng-repeat="d in data">
<p app-directive="d"></p>
</li>
</ul>
</div>
</body>
</html>
Run code
source
share