简体   繁体   中英

Access parent scope in angular directive with template

I woud like to reuse a form to edit one type of property. I have a list of editors:

ModelerControllers.controller('ModelController', ['$rootScope', '$scope', '$http', '$q',
    function ($rootScope, $scope, $http, $q) {
        ...
        $scope.mappingTypes = [
            {"name": "mixpanel", "label": "Mixpanel"},
            {"name": "mongo", "label": "Mongo"},
            {"name": "slicer", "label": "Slicer"},
            {"name": "sql", "label": "SQL"}, 
            {"name": "", "label": "Other"}
        ];
        ...
    }
]);

Then I have a directive:

CubesModelerApp.directive("mappingEditor", function() {
    return {
        templateUrl: 'views/partials/mapping.html',
        require: "ngModel",
        scope: {
            content: "=ngModel",
            mappingTypes: "@mappingTypes"
        },
        link: function($scope, $element, $attrs) {
            $scope.mappingTypes = $scope.$parent.mappingTypes;
        }
    }
});

Used as:

<div mapping-editor ng-model='content.mapping'></div>

With template content (relevant chunk):

<!-- language: html -->

...
<select class="form-control"
        ng-change="mappingTypeChanged(storeType)"
        ng-model="storeType"
        ng-options="f.name as f.label for f in mappingTypes">
</select>
...

This yields empty list – as if the mappingTypes was empty.

The conent from ngModel is bound correctly.

How do I access kind-of-global (from one of the parent scopes) enumeration in a directive template? Or is there any other way to achieve this, such as defining the list differently instead of app $scope variable?

EDIT: Here is a fiddle .

There were a couple of issues with your code:


1.
According to the docs regarding the isolated scope of custom directives:

= or =attr - set up bi-directional binding between a local scope property and the parent scope property of name defined via the value of the attr attribute

(emphasis mine)

This means that you need to reference an attribute whose value is the name of the parent scope property you want to share. Eg:

...
<editor ng-model="content" my-items="items"></editor>

...
scope: {
    ...
    items: '=myItems'
},

Alternatively, you could do the binding in the directive;s link function:

...
<editor ng-model="content"></editor>

...
scope: {
    content: 'ngModel'
    // nothing `items` related here
},

...
link: function (scope, element, attrs) {
    scope.items = scope.$parent.items;
}

2.
According to the docs regarding the select element, the ng-model attribute is mandatory. You have to add it into your template string:

...
template:
    ...
    '<select ng-model="selectedItem"...

3.
According to the same docs regarding the select element, the ng-options attribute's value must have one of the specified forms, which you fail to provide (see the docs for moe info on the allowed forms). For the simple case at hand, the template string could be modified like this:

...ng-options="item for item in items"...
               \______/
                   |_____(you need to have a label)

See, also, this short demo .

Try to set the argument mapping-types something like this

<div mapping-editor mapping-types='storeTypes' ng-model='content.mapping'></div>

If you set scope object in the directive, that means that this scope is isolated, and I'm not sure that you are able toe reach the parent scope. from the $parent object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM