简体   繁体   English

Scope函数没有收到回调参数

[英]Scope function isn't receiving a callback argument

I have a controller like this: 我有一个像这样的控制器:

angular.module('main', []).controller('mainCtrl', function ($scope) {
    $scope.doStuff = function (cb) {
        // Do some stuff.
        cb();
    };
});

And I have a directive like this: 我有这样的指示:

angular.module('action-bar', []).directive('actionBar', function () {
    return {
        restrict: 'EA',
        replace: true,
        template: '<h1>test</h1>',
        scope: {
            doStuff: '&'
        },
        link: function (scope, element, attrs) {
            scope.doStuff(function () {
                alert('callback executed');
            });
        }
    };
});

Here is the markup: 这是标记:

<div ng-app="main">
  <div ng-controller="mainCtrl">
    <div ng-action-bar></div>
  </div>
</div>

I can call doStuff just fine, however the cb argument is always undefined even though I'm passing in an anonymous function as the callback. 我可以调用doStuff ,但是cb参数总是undefined即使我传入一个匿名函数作为回调。 What am I doing wrong here? 我在这做错了什么?

Changing your isolate scope to use = instead of & allows you to access the parent scopes doStuff method. 将隔离范围更改为使用=而不是&允许您访问父范围doStuff方法。

angular.module('main', ['action-bar']).controller('mainCtrl', function ($scope) {
    $scope.doStuff = function (cb) {
        // Do some stuff.
        cb();
    };
});

angular.module('action-bar', []).directive('actionBar', function () {
    return {
        restrict: 'EA',
        replace: true,
        template: '<h1>test</h1>',
        scope: {
            doStuff: '='
        },
        link: function (scope, element, attrs) {
            scope.doStuff(function () {
                alert('callback executed');
            });
        }
    };
});

Using the directive in you markup 在您的标记中使用该指令

<div ng-app="main" ng-controller="mainCtrl">
    <action-bar do-stuff="doStuff">
    </action-bar>
</div>

Here is a working example . 这是一个有效的例子

If you want to use the expression ('&') syntax for passing doStuff into your directive then in your html you should pass it in like this: 如果你想使用表达式('&')语法将doStuff传递给你的指令,那么在你的html中你应该像这样传递它:

<div action-bar doStuff="doStuff(cb)"></div>

and in your directive's link function you can call it like this: 在你的指令的链接函数中,你可以像这样调用它:

scope.doStuff({cb: function() { ... }});

Checkout the egghead.io video on it for more info: https://egghead.io/lessons/angularjs-isolate-scope-expression-binding 查看egghead.io视频,了解更多信息: https ://egghead.io/lessons/angularjs-isolate-scope-expression-binding

Note: I personally don't like this syntax so I always use Jonathan Palumbo's solution (two way binding). 注意:我个人不喜欢这种语法,所以我总是使用Jonathan Palumbo的解决方案(双向绑定)。

I solved this by simply using scope.$parent to access the parent scope. 我通过简单地使用scope.$parent来访问父作用域来解决这个问题。 I don't actually need the parent function to be part of the directive's isolate scope. 我实际上并不需要将父函数作为指令的隔离范围的一部分。

Example: 例:

var app = angular.module('app', []);

// Main controller.
app.controller('mainCtrl', function ($scope) {
  // Initial text added to log.
  $scope.logs = ['App loaded.'];
  // A log function to add additional lines to the log.
  $scope.log = function (text, callback) {
    $scope.logs.push(text);
    if (callback) callback();
  };
});

// Directive to log lines to the log viewer.
app.directive('ngLogLine', function () {
  return {
    restrict: 'E',
    template: '<div><input type="text" ng-model="msg" ng-keydown="$event.keyCode === 13 ? log() : null" /><button ng-click="log()">send</button></div>',
    replace: true,
    scope: {},
    link: function (scope, element, attrs) {
      // Create our own log function on our isolate scope.
      scope.log = function () {
        // Call parent log function and pass in a callback.
        scope.$parent.log(scope.msg, function () {
          // Clear the msg, clearing the input.
          scope.msg = '';
        });
      };
    }
  };
});

Codepen: http://codepen.io/Chevex/pen/eHuaK/ Codepen: http ://codepen.io/Chevex/pen/eHuaK/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM