Frej
Frej

Reputation: 320

How do I connect between parent $scope, directive $scope and a modal $scope?

I'm trying to create a directive for a label selector.

<label-selector label="label1">label1</label-selector>

label1 is the default label that should be selected, this value comes from the $scope in the controller for the view where the label-selector is present.

The behaviour I want is as follows, when a user clicks label1 a modal should open. This modal lists a collection of labels, (the collection should be loaded from some array somhere). label1 in the collection should be marked as selected, e.g. class="selected".

When I click another label say, label2, label2 should be selected and the modal should be closed. This event will also update label-selector.

<label-selector label="label2">label2</label-selector>

I would like to encapsulate both the label-selector element and the modal in the same directive.

Is this possible?

UPDATE

I'v done a small example here.

HTML

 <div ng-app="app" id="app">
    <div ng-controller="MainCtrl">
        <label-select color="{{color}}"></label-select>
    </div>
</div>

CSS

.flyout {
    position: absolute;
    top: 0px;
    right: 0px;
    z-index: 31101;
    background-color: lightgray;
    bottom: 0px;
    box-shadow: -4px 0 4px rgba(0, 0, 0, 0.2);
    overflow-y: auto;
    width: 500px;
    -webkit-transform: translate(100%, 0%);    
    -webkit-transition: all 0.2s ease-in-out;
}
.flyout.show{
    -webkit-transform: translate(0%, 0%);    
}

JS

var app = angular.module('app', []);

app.controller('MainCtrl', function ($scope) {
    $scope.color = "#cecece";
});

app.directive('labelSelect', function ($parse, $location) {
    return {
        restrict: 'E',
        scope: {
            color: '@color'
        },
        template:
        '<a ng-click="selectColor()" style="color: {{color}}" class="label-color icon-bookmark">{{color}}</a><div class="flyout"></div>',
        link: function (scope, lElement, attrs) {
            scope.selectColor = function () {
                angular.element(".flyout").addClass("show");
            };            

        }
    }
});

http://jsfiddle.net/FrejNorling/bNumc/4/

The behaviour I try to create is that I want the flyout div to be filled with a list (ul list) of labels, and when I select one label in the list the the $scope.color in MainCtrl should be updated and the flyout should disapear/close.

Upvotes: 0

Views: 376

Answers (1)

dcodesmith
dcodesmith

Reputation: 9614

Like I said in the comment you can access the variables from the parent scope in the modal popups scope.

HTML

<div ng-app="app" id="app">
    <div ng-controller="MainCtrl">
        <label-select color="{{color}}"></label-select>
        <div class="flyout">
            <ul>
                <li ng-click="setColor(color)" ng-repeat="color in colors">{{color}}</li>
            </ul>
        </div>
    </div>
</div>

JS

var app = angular.module('app', []);

app.controller('MainCtrl', function ($scope) {
    $scope.color = "#AAADDD";
    $scope.colors = ["#AAA", "#BBB", "#CCC", "#DDD", "#EEE"];
    $scope.setColor = function (color) {
        angular.element(".flyout").removeClass("show");
        $scope.color = color;
    }

});

app.directive('labelSelect', function ($parse, $location) {
    return {
        restrict: 'E',
        scope: {
            color: '@color'
        },
        template:
            '<a ng-click="selectColor()" ng-showstyle="color: {{color}}" class="label-color icon-bookmark">{{color}}</a>',
        link: function (scope, lElement, attrs) {
            scope.selectColor = function () {
                angular.element(".flyout").addClass("show");
            };
        }
    }
});

JSFIDDLE DEMO

Upvotes: 1

Related Questions