相关疑难解决方法(0)

AngularJS:使用不同父级重用组件

假设我有A和B可重用组件.我希望B在A上调用一个方法 - 当然只有当B是A的孩子时.另外 - 我希望B用作独立组件(没有A作为父组件).在这种情况下,不应调用来自不存在的A的方法.我的第一个尝试是获取链接功能中的控制器(完全与我require: "^A"在B上指定的方式相同- 现在我不能拥有它,因为我想将B也用作独立组件) - 但这不起作用:

var module = angular.module("mymodule", []);

// <A>
module.directive("A", function() {
    return {
        restrict: "E",
        transclude: true,
        replace: true,
        scope: {},
        controller: function($scope, $element, $attrs) {
            this.register = function() {
               console.log("Hooray!");
            };
        },
        template:
            '<div class="ng-a" ng-transclude></div>'
    };
});

// <B>
module.directive("B", function() {
    return {
        restrict: "E",
        transclude: true,
        replace: true,
        scope: {},
        link: function($scope, $element, $attrs, refA) {
            if (refA !== undefined) {
                refA.register();
            } else {
                console.log("Standalone"); …
Run Code Online (Sandbox Code Playgroud)

angularjs

2
推荐指数
1
解决办法
1059
查看次数

标签 统计

angularjs ×1