javascript - 当指令 Controller 使用 Angular 读取它时,指令链接中的范围变量不会更新

标签 javascript jquery angularjs angularjs-directive

这是我的代码

    var directive = {
        restrict: 'E',
        templateUrl: '/static/myTemplate.html',
        scope: {
        },
        controller: ["$scope", controllerFunc],
        controllerAs: 'multiCheckboxCtrl',
        bindToController: true,
        link: linkFunc,

    };

    return directive;


    function controllerFunc($scope) {

        // $watch List

        /**
         * Event handler for searchText change
         */
        $scope.$watch(angular.bind(this, function () {
            return $scope.multiCheckboxCtrl.clickedElsewhere.value;
        }), function (newVal) {
            if (newVal !== undefined && newVal !== "") {
                console.log(newVal);
                console.log("I am here");
            }
        });


    }

    function linkFunc(scope, element, attr){

        // Detect Element Click Logic
        scope.multiCheckboxCtrl.clickedElsewhere = {value: false};

        $document.on('click', function(){
            scope.multiCheckboxCtrl.clickedElsewhere.value = false;
            console.log(scope.multiCheckboxCtrl.clickedElsewhere);
        });

        element.on('click', function(){
            event.stopPropagation();
            scope.multiCheckboxCtrl.clickedElsewhere.value = true;
            console.log(scope.multiCheckboxCtrl.clickedElsewhere);
        });
        // End Detect Element Click Logic

    }

linkFunc 基本上决定了指令元素是否被单击。

我验证了只要单击指令元素,它就会控制台为 true,而当单击指令元素之外的任何元素时,它会控制台为 false。

但是 Controller 中的 $watch 似乎没有捕获更改。

谁能告诉我出了什么问题

谢谢

最佳答案

Angular 不知道事件监听器中的对象属性发生了更改。

documentation 说:

$apply() is used to execute an expression in angular from outside of the angular framework. (For example from browser DOM events, setTimeout, XHR or third party libraries). Because we are calling into the angular framework we need to perform proper scope life cycle of exception handling, executing watches.

jsfiddle 上的实例.

angular.module('ExampleApp', [])
  .controller('ExampleController', function($scope) {
   
  })
  .directive('clickDirective', function() {
    var directive = {
      restrict: 'E',
      template: '<div>click me</div>',
      scope: {},
      controller: ["$scope", controllerFunc],
      controllerAs: 'multiCheckboxCtrl',
      bindToController: true,
      link: linkFunc,

    };

    return directive;


    function controllerFunc($scope) {

      // $watch List

      /**
       * Event handler for searchText change
       */
      $scope.$watch(function() {
        return $scope.multiCheckboxCtrl.clickedElsewhere.value;
      }, function(newVal) {
        if (newVal !== undefined && newVal !== "") {
          console.log(newVal);
          console.log("I am here");
        }
      });


    }

    function linkFunc(scope, element, attr) {

      // Detect Element Click Logic
      scope.multiCheckboxCtrl.clickedElsewhere = {
        value: false
      };

      angular.element(document).on('click', function() {
        scope.multiCheckboxCtrl.clickedElsewhere.value = false;
        console.log(scope.multiCheckboxCtrl.clickedElsewhere);
        scope.$apply();
      });

      element.on('click', function() {
        event.stopPropagation();
        scope.multiCheckboxCtrl.clickedElsewhere.value = true;
        console.log(scope.multiCheckboxCtrl.clickedElsewhere);
        scope.$apply();
      });
      // End Detect Element Click Logic

    };
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="ExampleApp">
  <div ng-controller="ExampleController" id="ExampleController">
    <click-directive></click-directive>
    <div>
       click elsewhere
    </div>
  </div>
</div>

关于javascript - 当指令 Controller 使用 Angular 读取它时,指令链接中的范围变量不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35759569/

相关文章:

javascript - 将一键单击事件处理程序应用于多个元素

javascript - jQueryUI 与 jQuery 3 兼容吗?迁移插件显示弃用

javascript - 从查询参数中获取所有范围变量

javascript - function.apply(argument1, array) 不从数组中获取所有值

javascript - React中post请求成功后重定向的正确方法是什么?

javascript - JavaScript 冲突

javascript - PHP 中如果为 null 则隐藏 div

javascript - 是否有一种解决方法可以将对象直接获取到指令范围?

javascript - dom的样式属性Angular js中的bind属性

javascript - jQuery 获取每个元素的每个背景图像 URL 并将其包装在 href 中