【问题标题】:How to revive an angular scope after destroy?销毁后如何恢复角度范围?
【发布时间】:2016-08-22 15:30:37
【问题描述】:

我想在内部作用域被销毁后重新编译它。

我正在使用重新编译功能来帮助,这是“嵌入”为真。 每当我想破坏内部作用域时,它也会破坏父(外部)作用域。

如何在保持重新编译功能不变的同时避免删除父作用域?

  function compile() {
      transclude(scope, function(clone, clonedScope) {
           previousElements = clone;
          $el.append(clone);

            $timeout(function () {

              scope.callBack();
    alert("Df");
}, 0);
    });
  }


 $scope.callBack = function(){

      var elem = document.getElementById('test');
      //This code kill the test div, but also the parent scope
      //Can i only kill the test div scope?
   angular.element(elem).scope().$destroy();
  alert("dd");
}

这里是 plunker: http://plnkr.co/edit/MePu8BipkkdHV0IEatbo

【问题讨论】:

  • 你真的不应该破坏一个范围,除非你是创造它的人。
  • 我很确定这不是实现您尝试的方法。你的目标是什么?

标签: angularjs angular-directive


【解决方案1】:

它正在破坏父作用域,因为您实际上从未创建过子作用域。

您可以尝试将scope: true 添加到kcdRecompile 指令中。

app.directive('kcdRecompile', ['$parse','$timeout', function($parse, $timeout) {
  'use strict';
  return {
    transclude: true,
    scope: true,
    link: function link(scope, $el, attrs, ctrls, transclude) {
...

这将使kcdRecompile 拥有自己的范围。

如果您希望kcdRecompile 具有父作用域,但其被转入的内容具有自己的作用域,则可以使用scope.$new() 而不是scope 调用转入函数。

app.directive('kcdRecompile', ['$parse','$timeout', function($parse, $timeout) {
  'use strict';
  return {
    transclude: true,
    link: function link(scope, $el, attrs, ctrls, transclude) {
      var previousElements;

      //compile();

      function compile() {
        transclude(scope.$new(), function(clone, clonedScope) {
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    相关资源
    最近更新 更多