GVKun编程网logo

angularjs – 为什么ng-transclude的范围不是它的指令范围的子 – 如果指令有一个孤立的范围?

15

本篇文章给大家谈谈angularjs–为什么ng-transclude的范围不是它的指令范围的子–如果指令有一个孤立的范围?,同时本文还将给你拓展angularjs–Angular1.2不再允许在同一

本篇文章给大家谈谈angularjs – 为什么ng-transclude的范围不是它的指令范围的子 – 如果指令有一个孤立的范围?,同时本文还将给你拓展angularjs – Angular 1.2不再允许在同一个元素上使用多个隔离的范围指令?、angularjs – Angular指令 – 当另一个指令也作为隔离范围时更新父范围、angularjs – ng-if在transcluded范围内打破范围继承、angularjs – 一个元素的多个伪指令可以共享隔离的范围吗?等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

angularjs – 为什么ng-transclude的范围不是它的指令范围的子 – 如果指令有一个孤立的范围?

angularjs – 为什么ng-transclude的范围不是它的指令范围的子 – 如果指令有一个孤立的范围?

给定一个指令(container1)与transclude和一个孤立的范围,当指令链接时,我有这些范围:
Scope 004           <-- scope of the body
    Scope 005       <-- scope of directive container1
    Scope 006       <-- scope of the ng-transclude

我期望:

Scope 004            <-- scope of the body
    Scope 005        <-- scope of the directive
         Scope 006   <-- scope of the ng-transclude

如果同一个指令有一个共享作用域而不是一个孤立的作用域,我得到预期的结果。

这使我有一个问题,因为如果被转录的内容包含另一个指令(component1)与一个孤立的范围,我得到:

Scope 004             <-- scope of the body
    Scope 005         <-- scope of the directive
    Scope 006         <-- scope of the ng-transclude
          Scope 007   <-- scope of directive component1

我想使用像这样的指令:

<container1>
   <component1 data="objectExposedInContainer1"/>
</container1>

但是这不工作,在component1内部,$ scope.data是未定义的,因为objectExposedInContainer1不在正确的范围。

我有两个问题:

>为什么ng-transclude的范围不是它的指令范围的子元素,如果指令有一个孤立的范围?这是一个错误?
>如果它不是一个错误,容器指令如何传递数据到它的内容,如果不是通过设置属性,如我试过。

这里是一个不工作的示例:http://plnkr.co/edit/NDmJiRzTF9e5gw8Buht2?p=preview.因为Plunker是用Anguar构建的,所以很难用Batarang进行调试。我建议在本地下载代码。注释掉app.js的第10行,以使其使用共享作用域。

Why ng-transclude’s scope is not a child of its directive’s scope if the directive has an isolated scope?

ng-transclude旨在允许指令使用任意内容,隔离的范围旨在允许指令封装其数据。

如果ng-transclude没有保留这样的范围,那么任何被转录的任何内容都需要知道指令的实现细节(即它需要知道你创建的孤立范围上的可用内容)。

If it’s not a bug,how can a container directive pass data to it’s content,if not by setting attributes like I tried.

如果容器指令和包含的指令是耦合的 – 即你写了它们并且需要他们一起行动,那么它们应该通过共享控制器通信。

如果容器指令应该将内容注入到孩子的范围内(例如ng-repeat),那么你不应该使用一个孤立的范围。

角度文档很清楚的是什么行为应该是:

“In a typical setup the widget creates an isolate scope,but the transclusion is not a child,but a sibling of the isolate scope. This makes it possible for the widget to have private state,and the transclusion to be bound to the parent (pre-isolate) scope.”

angularjs – Angular 1.2不再允许在同一个元素上使用多个隔离的范围指令?

angularjs – Angular 1.2不再允许在同一个元素上使用多个隔离的范围指令?

我在Angular项目中有一些代码,它们使用两个具有隔离范围的独立指令.它们不需要共享范围,只需存在于同一元素上.它们都以稍微不同的方式改变DOM,并且重要的是绑定到作为参数传递的值.

这在1.0中工作,但是Angular 1.2现在在尝试执行此操作时会生成错误

Multiple directives asking for new/isolated scope

基于项目git历史出现Angular 1.2更改行为,以保持两个隔离的指令在同一元素上分开.这是一件好事,当在同一元素上放置两个“Attribute”指令时,它可以正常工作.

<div my:directive="myDirectiveData" my:other-directive="myOtherDirectiveData" />

像你期望的那样工作.

然而

<my:directive my:directive-data="myDirectiveData" my:other-directive="myOtherDirectiveData" />

引发上述错误. (多个指令要求新/隔离范围)

在这种情况下,我希望每个指令仍然与它们自己的非共享隔离范围并行存在.

这在Angular 1.2中仍然可行吗?

在同一元素上定义多个指令时会发生什么情况的摘要:
  Scenario  directive #1   directive #2   Result
     1      no new scope   no new scope   Both directives use the controller's scope.
                                          (This should be obvIoUs.)
     2      new scope      new scope      Both directives share one new child scope.
     3      new scope      no new scope   Both directives share one new child scope.
                                          Why does dir #2 use the child scope?
                                          This seems odd to me.
     4      isolate scope  no new scope   Angular v1.0: both directives share the
                                          isolate scope.
                                          Angular v1.2+: dir #1 uses the isolate scope,dir #2 uses the controller's scope.

请注意,不允许以下方案(Angular引发错误):

  Scenario  directive #1   directive #2
     5      isolate scope  new scope
     6      isolate scope  isolate scope

angularjs – Angular指令 – 当另一个指令也作为隔离范围时更新父范围

angularjs – Angular指令 – 当另一个指令也作为隔离范围时更新父范围

我编写了一个需要更新父作用域的angular指令.
angular.module('app').directive('googlePlace',function () {
    return {
        restrict: 'A',require: 'ngModel',link: function ($scope,element,attributes,model) {

              $scope.property1 = 'some val';
              $scope.property2 = 'another val'; 
              $scope.$apply();

    };
});

但在我的控制器中我这样做:

MyCtrl = function($scope){
$scope.doSave = function(){
   // do some logic
   console.log($scope.property1);
   console.log($scope.property2);


}

}

当doSave运行时,我在控制台中得到未定义的值.如何在不隔离范围的情况下将其应用于父节点范围.我没有此选项,因为同一元素上的另一个指令隔离了作用域.

它应该工作.默认情况下,如果未在指令中指定范围,则使用父范围,因此应设置property1和property2.尝试将指令中的范围设置为false.作为旁注并不是一个好的做法,你在做什么.最好隔离范围并将属性添加为属性.这样你就会有很好的封装.

例如

angular.module('app').directive('googlePlace',scope: {
            property1: '=',property2: '='
        }
        link: function ($scope,model) {

            //here you have access to property 1 and 2

    };
});

function MyCtrl($scope) {
    $scope.property1 = null;
    $scope.property2 = null;

    $scope.doSave = function(){
   // do some logic
       console.log($scope.property1);
       console.log($scope.property2);
    }
}

还有你的HTML

<div ng-control="MyCtrl">
<div google-place property1='property1' property2='property2'></div>
</div>

angularjs – ng-if在transcluded范围内打破范围继承

angularjs – ng-if在transcluded范围内打破范围继承

这个小提琴说明了问题 http://jsfiddle.net/LAqeX/2/

我想创建一个包含页面一部分并隐藏它的指令.我想使用ng-if删除不必要的绑定.但是一些黑魔法发生了.

这是我最喜欢的指令代码.

app.directive('withIf',function(){
    return {
        restrict: 'E',scope: {
            title: '@'
        },transclude: true,template: '<div>' +
        '<p ng-click="visible = !visible">{{title}}</p>' +
        '<div ng-if="visible" ng-transclude></div>'+
        '</div>',link: function(scope){
            scope.visible = false;
        }
    }
});

它应该创建两个范围:

>指令隔离范围有两个变量 – 标题和可见
> Transcluded范围,原型继承自“常规”范围树.

然而,ng-if使得被转移的范围有点脱离现实,并且trasncluded范围不会从控制器继承.请看小提琴,它说明问题非常明确.

任何想法在那里发生了什么以及如何解决它?

UPDATE

我似乎已经找到了范围链看起来破碎的原因.由ng-if创建的范围属于withIf指令isolate分支.所以它永远不知道控制器的范围是否存在.但问题仍然是相同的 – 如何在这种情况下使用ng-if.

解决方法

ng-if创建子范围,使用$parent从父范围访问变量.但在你的情况下,我会考虑使用ng-show或ng-hide而不是ng-if(它们不创建子范围)

angularjs – 一个元素的多个伪指令可以共享隔离的范围吗?

angularjs – 一个元素的多个伪指令可以共享隔离的范围吗?

同一元素上的两个指令不能同时具有隔离范围,但是它们都可以使用与其父代隔离的相同范围.他们都可以使用绑定到隔离范围的属性吗?

例如,如果我有一个元素的两个指令

<eDirective aDirective prop="parentProp"/>

一个指令定义了一个带有绑定属性的隔离范围

App.directive('eDirective',function() {
  return {
    restrict: 'E',scope: {
      localProp: '=prop'
    },...
  };
});

其他指令是否获得该范围,并且可以使用绑定属性?

App.directive('aDirective',function() {
  return {
    restrict: 'A',link: function postLink(scope,element,attrs) {
        scope.$watch('localProp',function(newProp,oldProp) {
          ...
        }
    },...
  };
});

我的初始尝试(几乎编码如上)失败了.

我建议您通过辅助指令的require属性来使用指令控制器之间的通信.第一个指令(e-directive)保存隔离的作用域,而第二个helper指令(a-directive)具有对第一个伪指令的引用,并通过第一个伪指令定义的函数来设置属性.一个小样本将是( see plunker):
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <Meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.2.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js" data-semver="1.2.16"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <div e-directive config="parentConfig" a-directive></div>
  </body>

</html>

和javascript:

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

app.controller('MainCtrl',function($scope) {
  $scope.parentConfig = {};
});

app.controller('ECtrl',function ( $scope ) {
  this.setProp = function(newProp){$scope.config.prop = newProp;};

  $scope.$watch('config',oldProp) {
    console.log(oldProp,newProp);
  });
});

app.directive('eDirective',scope: {
      config: '='
    },controller: 'ECtrl',link: function(scope,attrs) {
      scope.config.prop ="abc";
    }
  };
});

app.directive('aDirective',require: 'eDirective',attrs,ctrl) {
        ctrl.setProp("def");
    }

  };
});

今天关于angularjs – 为什么ng-transclude的范围不是它的指令范围的子 – 如果指令有一个孤立的范围?的讲解已经结束,谢谢您的阅读,如果想了解更多关于angularjs – Angular 1.2不再允许在同一个元素上使用多个隔离的范围指令?、angularjs – Angular指令 – 当另一个指令也作为隔离范围时更新父范围、angularjs – ng-if在transcluded范围内打破范围继承、angularjs – 一个元素的多个伪指令可以共享隔离的范围吗?的相关知识,请在本站搜索。

本文标签: