Javascript AngularJS:在指令中从控制器执行函数

Javascript AngularJS:在指令中从控制器执行函数,javascript,html,angularjs,Javascript,Html,Angularjs,我对AngularJS非常陌生,我尝试做以下几点: 使用自定义指令绑定元素上的滚动事件。 代码如下: 首先,我的控制器: var officeUIApplication = angular.module('OfficeUI.Ribbon.Module', []) .controller('OfficeUI', ['$scope', '$http', function($scope, $http) { var application = this; $scope.customAle

我对AngularJS非常陌生,我尝试做以下几点:

使用自定义指令绑定元素上的滚动事件。 代码如下:

首先,我的控制器:

var officeUIApplication = angular.module('OfficeUI.Ribbon.Module', [])
.controller('OfficeUI', ['$scope', '$http', function($scope, $http) {
    var application = this;
    $scope.customAlert = function() {
        console.log('This ia scrolling demo.');
    }
}])
您会注意到,这里有一个名为“CustomAlert”的函数。我不知道为什么要将它绑定到$scope,我只在下一个版本中找到了此类信息。我可以移除这个范围吗?或者有人可以解释我为什么它很重要

然后我有指令:

.directive("ngcScroll", function() {
    return {
        restrict: 'A',
        link: function (scope, element, attributes) {
            scope.customAlert();
            element.on('DOMMouseScroll mousewheel', function (e) {
                console.log('Element is being executed.');
            });
        }
    }
});
已执行console.log,因此这不是问题,它已执行,但在scope.customAlert()上我收到以下错误:

scope.customAlert is not a function.
我发现这样做的方法如下:

scope.$apply('customAlert()');
但是,我收到的$apply已经在进行中

有人知道我应该如何做到这一点吗

亲切的问候,

试试这个

1. 演示:

2. &运算符允许您在指令的父作用域上调用或计算表达式

演示:

3. 嗯

我已经找到了解决办法,其实很简单。我在一个错误的控制器中定义了模块,但现在它可以正常工作

这意味着代码看起来像:

控制器:

.controller('OfficeUI', ['$scope', '$http', function($scope, $http) {
    var application = this;

    // Load up the JSon data string containing the definition of the ribbon.
    $http.get('/OfficeUI/Resources/JSon/application.json').
        success(function (data) {
            application.title = data.Title;
            application.icons = data.Icons;
        }).
        error(function (data) {
            // An error has occured while retrieving the data, so write it away in the logs.
            console.log('An error occured while retrieving the data.');
        });
}])
指令:

.directive("ngcScroll", function() {
    return {
        restrict: 'A',
        link: function (scope, element, attributes) {
            element.on('DOMMouseScroll mousewheel', function (e) {
                scope.$apply(attributes['ngcScroll'], 'demo');
            });
        }
    }
});
HTML:


  • {{tab.Name} {{tab.Contents}

从指令代码内部看,这应该有效:

link: function (scope, element, attributes) {
    scope.$parent.customAlert();
    element.on('DOMMouseScroll mousewheel', function (e) {
        console.log('Element is being executed.');
    });
}
通过以下方式访问父作用域上的任何方法和属性:

scope.$parent
这里需要注意的是,虽然这是访问父作用域方法的一种非常直接的方法,但它非常不灵活,因为它总是假设父作用域上存在什么。使用Alexander建议的隔离作用域可以提供更大的灵活性。当您将指令放在不同的父作用域中时,您可以通过HTML属性将要调用的实际父方法传递到指令中。

var myApp=angular.module('myApp',['ngRoute']);
//指令('myDirective',function(){});
//工厂('myService',function(){});
控制器(“MyCtrl”[“$scope”、函数($scope){
$scope.$on(“接收”,函数(f,e){
警报(e);
});
}]);
myApp.directive(“ngcScroll”,function()){
返回{
限制:“A”,
链接:功能(范围、元素、属性){
元素.on('DOMMouseScroll mousewheel',函数(e){
范围:$emit(“receive”,“djfhdfdhfgdhf”);
});
}
}
});

可以使用$injector中的$rootScope键检索根范围。子作用域是使用$new()方法创建的。(大多数作用域是创建的…





可以使用$injector中的$rootScope键检索根作用域。子作用域是使用$new()方法创建的。(大多数作用域是创建的…





可以使用$injector中的$rootScope键检索根作用域。子作用域是使用$new()方法创建的。(大多数作用域是创建的…





可以使用$injector中的$rootScope键检索根作用域。子作用域是使用$new()方法创建的。(大多数作用域是创建的…





可以使用$injector中的$rootScope键检索根作用域。子作用域是使用$new()方法创建的。(大多数作用域是创建的…





可以使用$injector中的$rootScope键检索根作用域。子作用域是使用$new()方法创建的。(大多数作用域是创建的…





可以使用$injector中的$rootScope键检索根作用域。子作用域是使用$new()方法创建的。(大多数作用域是创建的…





可以使用$injector中的$rootScope键检索根作用域。子作用域是使用$new()方法创建的。(大多数作用域是创建的…





可以使用$injector中的$rootScope键检索根作用域。子作用域是使用$new()方法创建的。(大多数作用域是创建的…





可以使用$injector中的$rootScope键检索根作用域。子作用域是使用$new()方法创建的。(大多数作用域是创建的…





可以使用$injector中的$rootScope键检索根作用域。子作用域是使用$new()方法创建的。(大多数作用域是创建的…





可以使用$injector中的$rootScope键检索根作用域。子作用域是使用$new()方法创建的。(大多数作用域是创建的…





可以使用$injector中的$rootScope键检索根作用域。子作用域是使用$new()方法创建的。(大多数作用域是创建的…





可以使用$injector中的$rootScope键检索根作用域。子作用域是使用$new()方法创建的。(大多数作用域是创建的…





可以使用$injector中的$rootScope键检索根作用域。子作用域是使用$new()方法创建的。(大多数作用域是创建的…





可以使用$injector中的$rootScope键检索根作用域。子作用域是使用$new()方法创建的。(大多数作用域是创建的…





可以使用$injector中的$rootScope键检索根作用域。子作用域是使用$new()方法创建的。(大多数作用域是创建的…
var myApp = angular.module('myApp',[]); myApp.controller('MyCtrl', function ($scope, $rootScope) { $scope.customAlert = function() { console.log('This ia scrolling demo.'); } $scope.$on('customAlert', $scope.customAlert); }); myApp.directive("ngcScroll", function($rootScope) { return { restrict: 'A', link: function (scope, element, attributes) { element.on('DOMMouseScroll mousewheel', function (e) { scope.$emit('customAlert'); }); } } });
.controller('OfficeUI', ['$scope', '$http', function($scope, $http) {
    var application = this;

    // Load up the JSon data string containing the definition of the ribbon.
    $http.get('/OfficeUI/Resources/JSon/application.json').
        success(function (data) {
            application.title = data.Title;
            application.icons = data.Icons;
        }).
        error(function (data) {
            // An error has occured while retrieving the data, so write it away in the logs.
            console.log('An error occured while retrieving the data.');
        });
}])
.directive("ngcScroll", function() {
    return {
        restrict: 'A',
        link: function (scope, element, attributes) {
            element.on('DOMMouseScroll mousewheel', function (e) {
                scope.$apply(attributes['ngcScroll'], 'demo');
            });
        }
    }
});
<div ngc-scroll="enableTab(this)" class="ribbon officeui-borders-border-bottom-grey" ng-controller="OfficeUIRibbon as ribbon">
    <ul role="tabs" class="officeui-space-no-margin officeui-space-no-padding officeui-borders-border-bottom-grey">
        <li role="tab" ng-repeat="tab in ribbon.tabs" ng-class="{ application: $first, active: !$first && ribbon.isActive(tab.Id) }" class="officeui-display-inline-block" ng-click="$first || ribbon.setTab(tab.Id)">
            <span>{{tab.Name}}</span>
            <div id="{{tab.contentId}}" ng-class="{ 'officeui-display-block': !first && ribbon.isActive(tab.Id), 'officeui-display-hidden': !first && !ribbon.isActive(tab.Id)}" class="contents officeui-position-absolute">
                {{tab.Contents}}
            </div>
        </li>
    </ul>
</div>
link: function (scope, element, attributes) {
    scope.$parent.customAlert();
    element.on('DOMMouseScroll mousewheel', function (e) {
        console.log('Element is being executed.');
    });
}
scope.$parent