javascript - directive communication and coupling -



javascript - directive communication and coupling -

i have 2 directives, dropdown , panel.

panels contain dropdowns. using require : "^panel", , specifying controller, can create both communicate.

you can see illustration here

http://plnkr.co/edit/rqbsz26jiqkzfmrpbmkd?p=preview

class="snippet-code-html lang-html prettyprint-override"><!doctype html> <html ng-app="app"> <head> <script data-require="angular.js@*" data-semver="1.3.0-rc2" src="https://code.angularjs.org/1.3.0-rc.2/angular.js"></script> <link rel="stylesheet" href="style.css" /> <script src="script.js"></script> <script> angular .module("app", []) .controller("ctrl", function($scope) { $scope.test = "welcome" }) .directive("dropdown", function() { homecoming { require: "^panel", constraint: "e", transclude: true, scope: {}, template: "<div>{{selectedvalue}}<select ng-model='selectedvalue'><option>1</option><option>2</option></select></div>", link: function(scope, b, c, panelcontroller) { scope.selectedvalue = 1; scope.$watch('selectedvalue', function(newval, oldval) { panelcontroller.setvalue2(newval) }) } } }) .directive("panel", function() { homecoming { constraint: "e", transclude: true, controller: function($scope) { this.setvalue2 = function(val) { $scope.value2 = val; if (val == 1) { $scope.style1 = true; $scope.style2 = false; } if (val == 2) { $scope.style1 = !true; $scope.style2 = !false; } } }, template: "<div ng-class='{panel: true, style1: style1, style2: style2}'><h2>value: {{value2}}</h2><br><dropdown></dropdown></div>", scope: {}, link: function(scope, b, c) {} } }) </script> </head> <body ng-controller="ctrl"> <h1>{{test}}</h1> <panel></panel> <panel></panel> <panel></panel> </body> </html>

my question how create dropdown directive independent too, want two:

1) dropdown directive work itself, can set in html , 2) sometimes, within other directives panel

i thinking generate returned directive object and, if needed, add together object controller mention when needed.

is right way remove tight coupling have between panel , dropdown while allowing me still flexibility utilize dropdown in other scenario?

please find updated plunkr containing rahil recommendations, working

http://plnkr.co/edit/rqbsz26jiqkzfmrpbmkd?p=preview

javascript angularjs

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -