Javascript 角函数中链接函数的范围变量的范围是什么?

Javascript 角函数中链接函数的范围变量的范围是什么?,javascript,angularjs,angularjs-directive,angularjs-scope,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,在上面的代码片段中,我试图更改我在控制器中单击元素时声明的范围变量标题,但它没有显示任何更改 你们能帮我吗?提前感谢。您必须在范围内分配标题。$apply。作用域。$apply将使单击处理程序进入摘要循环 var $directive = angular.module('myApp', []); $directive.directive('myDirective', function(){ return { restrict: 'E', template: '<

在上面的代码片段中,我试图更改我在控制器中单击元素时声明的范围变量标题,但它没有显示任何更改


你们能帮我吗?提前感谢。

您必须在范围内分配标题。$apply。作用域。$apply将使单击处理程序进入摘要循环

var $directive = angular.module('myApp', []);
$directive.directive('myDirective', function(){
   return {
      restrict: 'E',
      template: '<h4>{{title}}</h4>'
      compile: function(element, attrs){
        console.log('this is compile'); 
        return function link(scope, element, attrs){ 
           element.on('click', function(){
              scope.title = 'My Directive 2 on click'; 
              scope.dataPoint(scope.title);
           }); 
        }; 
      },
     controller: function($scope){
        $scope.title = "My Directive";
         $scope.dataPoint = function(title){
             $scope.title = title;
         };
     }
  };
});
<my-directive></my-directive>
link: function(scope, element, attr, ctrl) {
    element.on('click', function() {
        scope.$apply(function() {
            scope.title = 'My Directive 2 on click'; 
        });
    });
}