简体   繁体   English

如何在隔离范围指令中访问此范围变量?

[英]How do I access this scope variable inside of isolated scope directive?

This is my situation in psuedo code这是我在伪代码中的情况

<div data-ng-controller="test">

    <div isolated-directive>
        <select ng-model="testControllerScopeVar">...</select>
    </div>

    <div ng-if="some condition that uses testControllerScopeVar"></div>

</div>

This worked perfectly before I added isolated-directive , now that it is added ( scope: true ) the ng-if no longer works because I think it is getting eat up inside of the directive.在我添加isolated-directive之前,这很有效,现在它被添加( scope: trueng-if不再有效,因为我认为它在指令内部被吃掉了。

What is the most efficient way to get this working without touching the structure of the html and isolated-directive ?在不触及 html 和isolated-directive的结构的情况下,最有效的方法是什么?

Well it seems once I know the solution, it is so simple好吧,一旦我知道解决方案,似乎就这么简单

<div data-ng-controller="test as testCtrl">

    <div isolated-directive>
        <select ng-model="testCtrl.testControllerScopeVar">...</select>
    </div>

    <div ng-if="testCtrl.testControllerScopeVar == 'whatever'"></div>

</div>

ControllerAs allows me to specifically access the right scope and works perfectly, thanks all for your time and input ControllerAs 允许我专门访问正确的范围并完美运行,感谢您的时间和投入

One approach is to map the controller variable into your isolated scope and attach the isolated scope variable to your internal ng-model .一种方法是将控制器变量映射到您的隔离范围并将隔离范围变量附加到您的内部ng-model

So your HTML would look like this:所以你的 HTML 看起来像这样:

<div data-ng-controller="test">

    <div isolated-directive="testControllerScopeVar">
        <select ng-model="isolatedScopeVar">...</select>
    </div>

    <div ng-if="some condition that uses testControllerScopeVar"></div>

</div>

And your directive declaration would look like this:您的指令声明如下所示:

app.directive('isolatedDirective', function () {
    return {
        scope: {
            isolatedScopeVar: '=isolatedDirective'
        }
    };
});

You can try jQuery to get the value and assign it to a new scope variable.您可以尝试使用 jQuery 获取值并将其分配给新的作用域变量。 Something like this像这样的东西

HTML HTML

<div ng-app="TestApp">
  <div data-ng-controller="test">
    <div isolated-directive>
      <input id="isolatedVar" ng-model="testControllerScopeVar" />
    </div>
    <div>
      {{isolatedVar}}
    </div>
  </div>
</div>

JS JS

   var app = angular.module('TestApp', []);
    app.controller('test', function($scope) {
      var element = angular.element(document.querySelector('#isolatedVar'));
      element.bind('keyup', function() {
        $scope.isolatedVar = element.val();
        console.log($scope.isolatedVar);
        $scope.$watch('isolatedVar', function() {});

      });

    });
    app.directive('isolatedDirective', function() {
      return {
        scope: true
      };
    });

Working fiddle https://jsfiddle.net/kavinio/yzb8ouzd/1/工作小提琴https://jsfiddle.net/kavinio/yzb8ouzd/1/

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

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