如何在父指令和子指令之间共享范围

时间:2017-03-16 12:27:10

标签: angularjs scope angular-directive

我正在创建一个指令,其中两个其他子指令使用它是功能但我需要帮助知道如何在父指令和子指令之间共享范围

家长指示

MetronicApp.directive('atmSearchComponent', function () {

    function AtmSearchComponentController(scope) {
        // initialize scope
    }

    AtmSearchComponentController.prototype.search = function () {
       scope.mainGridOptions.dataSource.page(1);
       reset();
    }

    AtmSearchComponentController.prototype.clearSearch = function () {
       scope.data.basicSearch = {};
       scope.data.advancedSearch = {};
    }

    AtmSearchComponentController.prototype.keyPressSearch = function (keyEvent) {
       if (keyEvent.which === 13) {
           keyEvent.preventDefault()
           scope.search();
       }
    };

    return {
        restrict: 'E',
        controller: ['$scope', AtmSearchComponentController],
        scope: {}
    };

});

儿童指令

MetronicApp.directive('atmsQuerySelector', function ($log, $parse, $timeout, $http, $uibModal) {
    return {
        restrict: "E",
        replace: false,
        scope: {},
        require: ['^atmSearchComponent', 'ngModel'],
        templateUrl: "/apps/framework/templates/atmsSelector.view",
        link: function (scope, element, attrs, controllersArray) {

            scope.search = controllersArray[0].search;

            clearSearch = controllersArray[0].clearSearch;

            scope.keyPressSearch = controllersArray[0].keyPressSearch;
        },
    }
});

但是当我调用搜索方法时,我收到以下错误

  

ReferenceError:未定义范围       at n.AtmSearchComponentController.search(directives.js:8)

0 个答案:

没有答案