javascript - 如何仅对 Angular 中的某些变量使用父范围,并保持其他变量隔离

标签 javascript angularjs angularjs-directive angularjs-scope

mydirective 是一个独立的作用域指令。 这是因为我不想将指令的所有逻辑暴露给指令之外的任何地方。 但是我想在指令之外访问输入数据。

<div mydirective>
   <input ng-model="data.input">
</div>

<div mydirective>
   <input ng-model="otherdata.public">
   <input ng-model="more.than.one">
</div>

{{data.input}}
{{otherdata.public}}

我更喜欢 HTML 无需更改就可以工作,并且只更改指令代码。所以我想知道如何创建指令

app.directive('mydirective',function(){ return {
 scope:true,
 controller:function($scope){
     $scope.this_variable_needs_to_be_private=true
 },
 transclude:true
}})

编辑:添加 transclude:true。但我仍然没有答案。

最佳答案

考虑使用 $transclude使用 $scope.$new() 创建您自己的 childScope 函数:

(function() {
  "use strict";

  angular.module("myApp", [])
    .controller("Controller1", ['$scope', Controller1])
    .directive("mydirective", [mydirective]);

  function Controller1($scope) {
    $scope.data = {
      input: 'data.input'
    };
    $scope.otherdata = {
      public: 'otherdata.public'
    };
    $scope.more = {
      than: {
        one: 'more.than.one'
      }
    }
  }

  function mydirective() {
    function _link($scope, $element, $attrs, controller, $transclude) {
      var childScope = $scope.$new(); //create a childScope

      //$scope.this_variable_needs_to_be_private = true; //<-- doing this would add it to public parent scope
      childScope.this_variable_needs_to_be_private = true; //<-- this puts it only on this directive's childscope

      // See: https://docs.angularjs.org/api/ng/service/$compile#transclusion
      $transclude(childScope, function(clone, scope) { //transcluding with our childScope
        $element.append(clone); //appending the clone of our content;
      });
    }

    return {
      transclude: true,
      link: _link
    };
  }

})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js"></script>
<div ng-app="myApp" ng-controller="Controller1">

  <div mydirective>
    <input ng-model="data.input">
    <br /><strong>this_variable_needs_to_be_private:</strong> {{this_variable_needs_to_be_private}}
  </div>

  <br />

  <div mydirective>
    <input ng-model="otherdata.public">
    <input ng-model="more.than.one">
    <br /><strong>this_variable_needs_to_be_private:</strong> {{this_variable_needs_to_be_private}}
  </div>

  <hr />
  <strong>data.input:</strong> {{data.input}}
  <br /><strong>otherdata.public:</strong> {{otherdata.public}}
  <br /><strong>this_variable_needs_to_be_private:</strong> {{this_variable_needs_to_be_private}}


</div>

进一步阅读 $transclude:https://stackoverflow.com/a/13184889/446030 .

关于javascript - 如何仅对 Angular 中的某些变量使用父范围,并保持其他变量隔离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31559007/

相关文章:

javascript - 我收到 Uncaught TypeError 错误?

javascript - XMLHttpRequest 在 Chrome 应用程序中不起作用

javascript - 如何绘制跨越圆周点的平滑路径

javascript - Angularjs 双向数据绑定(bind)不起作用; $watch 也不起作用

javascript - 如何为 Ng Repeat 指令访问的 View 中的每个项目创建“查看更多”切换

javascript - 格式化 AngularJS 指令模板的方法

javascript - 为什么在 JS 中 +str 比 str*1 更好地将字符串转换为数字?

angularjs - angular jasmine 多个嵌套的 httpbackend 调用

html - 单击div并将某些对象传递给窗口后如何使用angular js打开弹出窗口

javascript - angularjs - 在运行时创建指令实例?