简体   繁体   English

为什么`replace:true`在AngularJS指令中不推荐使用

[英]Why is `replace: true` Deprecated in AngularJS Directive

I am working on a sample player using angularjs, utilizing an element directive. 我正在使用angularjs处理一个使用元素指令的示例播放器。 I would like the events within the directive's template to remain contained within the directive. 我希望指令模板中的事件仍然包含在指令中。 In other words, how can I use the controller in the directive to create events scoped only to elements within the directive's template. 换句话说,如何在指令中使用控制器来创建仅限于指令模板中元素的事件。

The directive: 指令:

logsApp.directive('logPlayer', function() {
    return {
        restrict: 'E',
        replace: true,
        scope: {audio: '='},
        template: '<div ng-repeat="sample in audio">' +
            '{{sample.title}}' +
            '<button type="button" class="btn btn-default" ng-click="play()">Play</button>' +
            '<button type="button" class="btn btn-danger" ng-click="stop()">Stop</button>' +
            '</div>'
    };
});

The only way I can get the ng-click events to work is place the event methods in the parent controller's $scope : 我可以让ng-click事件工作的唯一方法是将事件方法放在父控制器的$scope

logsApp.controller('LogListController', ['$scope', '$rootScope', 'LogsService', 'SoundManager', function($scope, $rootScope, LogsService, SoundManager) {
    ...
    $scope.play = function() {
    SoundManager.sound.createSound({
        id: SoundManager.idPrefix + this.sample.id,
        url: this.sample.filename,
        autoLoad: true,
        autoPlay: false,
        onload: function() {
            this.setPosition(0);
            this.play({
                whileplaying: function() {
                    ...
                }
            });
        }
    });
};

$scope.stop = function() {
    SoundManager.sound.stopAll();
};

How would I accomplish getting the play() and stop() events to be contained within the directive controller? 如何将指令控制器中包含的play()stop()事件完成?

When I create a controller to the directive, and apply $scope.play = function() {}; 当我为指令创建一个控制器,并应用$scope.play = function() {}; nothing happens. 什么都没发生。

The problem that you are having is that you are using replace: true which is deprecated. 您遇到的问题是您正在使用不推荐使用的replace: true Remove that and your directive's controller will see the click events. 删除它,您的指令的控制器将看到单击事件。

angular.module("myApp").directive('logPlayer', function() {
    return {
        restrict: 'E',
        //replace: true,
        scope: {audio: '='},
        controller: function($scope) {
            $scope.play = function(index) {
                console.log("play clicked "+index);
            };
        },
        template: '<div ng-repeat="sample in audio">' +
        '{{sample.title}}' +
        '<button type="button" ng-click="play($index)">Play</button>' +
        '<button type="button" ng-click="stop($index)">Stop</button>' +
        '</div>'
    };
});

The DEMO on JSFiddle . JSFiddle上DEMO

From the Docs: 来自Docs:

replace ([DEPRECATED!], will be removed in next major release - ie v2.0) replace ([DEPRECATED!],​​将在下一个主要版本中删除 - 即v2.0)

specify what the template should replace. 指定模板应替换的内容。 Defaults to false . 默认为false

  • true - the template will replace the directive's element. true - 模板将替换指令的元素。
  • false - the template will replace the contents of the directive's element. false - 模板将替换指令元素的内容。

-- AngularJS Comprehensive Directive API - AngularJS综合指令API

From GitHub: 来自GitHub:

Caitp-- It's deprecated because there are known, very silly problems with replace: true , a number of which can't really be fixed in a reasonable fashion. Caitp--它被弃用了,因为有一些已知的,非常愚蠢的replace: true问题replace: true ,其中一些不能以合理的方式真正修复。 If you're careful and avoid these problems, then more power to you, but for the benefit of new users, it's easier to just tell them "this will give you a headache, don't do it". 如果你小心并避免这些问题,那么对你有更多的权力,但为了新用户的利益,更容易告诉他们“这会让你头疼,不要这样做”。

-- AngularJS Issue #7636 - AngularJS问题#7636

In this case the isolate scope of the directive is fighting the inherited scope of the ng-repeat directive. 在这种情况下,指令的隔离范围正在与ng-repeat指令的继承范围作斗争。 So either remove replace: true or remove the ng-repeat from the top element of the template. 因此,要么删除replace: true要么从模板的顶部元素中删除ng-repeat


Update 更新

Note: replace: true is deprecated and not recommended to use, mainly due to the issues listed here. 注意: replace: true已弃用且不建议使用,主要是由于此处列出的问题。 It has been completely removed in the new Angular. 它已在新的Angular中完全删除。

Issues with replace: true 替换问题:true

For more information, see AngularJS $compile Service API Reference - Issues with replace:true . 有关更多信息,请参阅AngularJS $ compile Service API Reference - 有关replace:true问题

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

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