【问题标题】:Requiring directive's controller from component return an empty controller从组件中请求指令的控制器返回一个空控制器
【发布时间】:2016-12-17 16:57:41
【问题描述】:

所以在过去的 4 个小时里,我一直在努力解决这个问题。基本上我有一个父指令被动态编译成 DOM 在其中我有一个正在被嵌入的组件

$compile('<edit-modal entry="entry" positions="positions" day="day" is-filled="isFilled" week-start-date="weekStartDate" available-tags="availableTags" minigrids="minigrids">' +
        '<ns-gap-requests gap="entry" minigrids="minigrids"></ns-gap-requests></edit-modal>')(scope);

下面是editModal html中的组件渲染:

 <div id="gapRequestsBody" ng-if="onGapRequest" ng-transclude></div>

以下是我的父指令

 return {
        restrict: 'E',
        replace: 'true',
        transclude:"true",
        templateUrl: 'Scripts/NewScheduler/Templates/Modals/editModal.html',

        scope: {
            positions: '<',
            entry: '=',
            day: '<',
            weekStartDate: '<',
            availableTags: '<',
            minigrids: '<'
        },

        controller: ['$scope', '$element', function ($scope, $element) {
            $scope.$parent.child = $scope;

            $scope.onGapRequest = false;
            $scope.changeToOnGapRequestPage = function() {
                $scope.onGapRequest = true;
            }

.....

以下是我的子组件:

(function() {
    'use strict';

    angular.module('newScheduler').component('nsGapRequests',
        {
            require:{editModalCtrl : "^editModal"},
            bindings: {
                gap: "=",
                minigrids:"<"
            },
            templateUrl: "Scripts/NewScheduler/Templates/Modals/nsGapRequestsModal.html",
            controller: [function () {
                var self = this;

                self.$onInit = function() {
                    console.log(self);
                }

                console.log(self.gap);
                console.log(self.minigrids);

                if (!self.assignToOption) {
                    self.assignToOption = { chosenRequester: null };
                }

                self.requesters = self.minigrids.map(function (minigrid) {
                    return minigrid.gridRows;
                }).reduce(function(a, b) {
                    return a.concat(b);
                })
                    .map(function (gridRows) {
                    return gridRows.user;
                    })
                .filter(function (value, index, array) {
                    return array.indexOf(value) === index;
                })
                .filter(function(user) {
                    return self.gap.requests.indexOf(user.id) !== -1;
                    });

            }],
            controllerAs: "gapRequests"
        });
})(); 

但我不明白为什么我无法访问父控制器: console log of the child component members

由于某种原因,editModalCtrl 是空的(但它不应该是!!!)

如果有人能在这方面帮助我,我将不胜感激。 干杯。

【问题讨论】:

    标签: javascript angularjs angularjs-directive angularjs-scope angularjs-components


    【解决方案1】:

    Controller 实际上不是空的,你只是没有在上面定义任何属性/方法。您正在使用 $scope 代替。尝试添加一些成员并检查:

    controller: ['$scope', '$element', function ($scope, $element) {
        var self = this;
        self.someMember = true;
        self.someMethod = function() {
        }
    }
    

    【讨论】:

    • 哦,你是对的。多谢了。但是有一个问题。我认为当您与另一个指令/组件共享指令的控制器时,您实际上是在共享范围,这不是真的吗?。
    • 不,除非您以某种方式明确地公开它。你也可以直接绑定到控制器而不是使用 $scope。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多