首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将属性从一个指令转移到另一个指令

将属性从一个指令转移到另一个指令
EN

Stack Overflow用户
提问于 2015-08-04 07:50:58
回答 2查看 781关注 0票数 0

我有指令foo,我想在另一个指令dropdown上使用它。问题是,dropdown指令在模板中使用了另一个名为kendo-drop-down-list的指令。

我想能写

代码语言:javascript
复制
<dropdown foo>

结果应该是

代码语言:javascript
复制
<select data-kendo-drop-down-list options='dropdownOptions' data-ng-model='selected' foo="bar"></select>

问题是foo是可选的,这意味着指令将同时被使用,比如<dropdown><dropdown foo="bar">

如何传递属性?还是我做错了什么,因为我结束了这个问题?

指令

代码语言:javascript
复制
app.directive('dropdown', function() {
    return {
        restrict: "AE",
        scope: {
            selected: "=ngModel",
        },
        template: "<select data-kendo-drop-down-list data-k-options='dropdownOptions' data-ng-model='selected'></select>",
        controller: [
            '$scope', function($scope) {                
                $scope.dropdownOptions = {
                    dataSource: {
                        type: "odata-v4",
                        transport: {
                            read: {
                                url: "odata/Products",
                                dataType: "json",
                            }
                        },
                        serverFiltering: true,
                    }
                };
            }
        ]
    };
);
EN

回答 2

Stack Overflow用户

发布于 2015-08-04 08:57:05

设法弄清楚了。受此https://stackoverflow.com/a/27261632/1220627的启发

代码语言:javascript
复制
app.directive('dropdown', function() {
    return {
        restrict: "AE",
        scope: {
            selected: "=ngModel",
        },
        template: function(element, attrs) {
            var templateHtml = "<select data-kendo-drop-down-list data-k-options='dropdownOptions' data-ng-model='selected'></select>";
            var templateElement = angular.element(templateHtml);

            _.pairs(attrs.$attr).forEach(function (pair) {
                var attributeName = pair[0];
                var attributeNameActual = pair[1];
                // ignore attribute(s) from scope
                if (attributeName === "ngModel")
                    return;

                var attributeValue = attrs[attributeName];
                templateElement.attr(attributeNameActual, attributeValue);
            });

            return templateElement;
        },
        controller: [
            '$scope', function($scope) {                
                $scope.dropdownOptions = {
                    dataSource: {
                        type: "odata-v4",
                        transport: {
                            read: {
                                url: "odata/Products",
                                dataType: "json",
                            }
                        },
                        serverFiltering: true,
                    }
                };
            }
        ]
    };
);
票数 0
EN

Stack Overflow用户

发布于 2015-08-04 09:13:45

您可以将控制器的$scope变量从directiveA传递给directiveB,如下所示:

代码语言:javascript
复制
var myApp = angular.module('myApp', []);

myApp.directive('directiveA', function() {
    return {
        restrict: 'E',
        template: '<directive-b foo="data"></directive-b>',
        scope: {
            data: '='
        }
    }
});

myApp.directive('directiveB', function() {
    return {
        restrict: 'E',
        template: '{{data}} I am Directive B!',
        scope: {
            data: '=foo'
        }
    }
});

myApp.controller('MyCtrl', function($scope) {
    $scope.foo = 'Hi there!';
});
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="MyCtrl">
    <directive-a data="foo"></directive-a>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31803576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档