【问题标题】:Angular isolated scope values not visible in template when using replace使用替换时在模板中不可见的角度隔离范围值
【发布时间】:2016-08-24 05:55:20
【问题描述】:

我正在创建一个小应用程序,并且我有以下带有模板的指令。

smallgrid.directive.js:

angular.module('myActions')
    .directive('smallgrid', ['$rootScope', function($rootScope) {
        return {
            restrict: "E",
            scope: {
                actionable: "="
            },
            controller: function($scope) {
                $scope.setLocation = function() {
                    console.log("yee");
                };
            }
        };
    }])
    .directive('three', function() {
        return {
            replace: true,
            templateUrl: '/app/my_actions/directives/templates/grid3x3.template.html'
        };
    })
    .directive('four', function() {
        return {
            replace: true,
            templateUrl: '/app/my_actions/directives/templates/grid4x4.template.html'
        };
    })
    .directive('five', function() {
        return {
            replace: true,
            templateUrl: '/app/my_actions/directives/templates/grid5x5.template.html'
        };
    });

grid3x3.template.html

<div class="k-edit-field" id="board">
    <div class="row" ng-click="setLocation()">
        {{actionable.probability}}
    </div>
</div>

我使用这个指令如下:

<smallgrid three actionable="currentAction.actionable" ng-if="somecondition"></smallgrid>

UI 正确呈现。但是,它显示 {{actionable.probability}} 为空,并且 Click 事件未触发。但是,如果我删除隔离范围并直接访问变量,则值是可用的。我知道当我使用隔离作用域时,在three 指令中,我无法访问smallgrid 的值。有没有办法将这些值从 smallgrid 传递到模板?

【问题讨论】:

    标签: javascript angularjs angularjs-directive angularjs-scope


    【解决方案1】:

    将指令作为指令的属性传递,您肯定会遇到范围问题。

    如果您将范围继承用于带有ng-transclude 的嵌套指令,效果会更好。

    所以你的出发点应该是

    <smallgrid actionable="currentAction.actionable" ng-if="somecondition">
      <three></three>
    </smallgrid>
    

    这样&lt;three&gt; 可以访问$parent

    function smallgrid() {
      return {
        restrict: "E",
        transclude: true,
        scope: {
          actionable: "="
        },
        template: `<div ng-transclude></div>`,
        controller: function($scope) {
          $scope.setLocation = function() {
            console.log("yee");
          };
        }
      };
    }
    function three() {
      return {
        template: `<div class="k-edit-field" id="board">
                    <div class="row" ng-click="$parent.setLocation()">
                      test = {{$parent.actionable.probability}}
                    </div>
                  </div>`
      };
    }
    function myController($scope) {
      $scope.currentAction = {actionable: {probability: "test"}};
      $scope.somecondition = true;
    }
    angular.module('myApp', []);
    angular
        .module('myApp')
        .controller('myController', myController)
        .directive('smallgrid', smallgrid)
        .directive('three', three);
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
    <div ng-app="myApp">
      <div ng-controller="myController">
        <smallgrid actionable="currentAction.actionable" ng-if="somecondition">
          <three></three>
        </smallgrid>
      </div>
    </div>

    【讨论】:

    • 太棒了。感谢您的描述性回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    相关资源
    最近更新 更多