Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 玩得漂亮。。如何在没有控制器的情况下在angular指令中使用此.someFunction()?_Angularjs_Ecmascript 6_Internet Explorer 11_Angular Directive - Fatal编程技术网

Angularjs 玩得漂亮。。如何在没有控制器的情况下在angular指令中使用此.someFunction()?

Angularjs 玩得漂亮。。如何在没有控制器的情况下在angular指令中使用此.someFunction()?,angularjs,ecmascript-6,internet-explorer-11,angular-directive,Angularjs,Ecmascript 6,Internet Explorer 11,Angular Directive,例如,此Angular指令在Chrome和Firefox中运行良好: // using Angular v1.4.8 myApp.directive('myDir', function(){ return { restrict: 'E', templateUrl: 'directive/myDir/myDir.html', bindToController: true, controllerAs: 'mydir', control

例如,此Angular指令在Chrome和Firefox中运行良好:

// using Angular v1.4.8
myApp.directive('myDir', function(){
   return {
      restrict: 'E',
      templateUrl: 'directive/myDir/myDir.html',
      bindToController: true,
      controllerAs: 'mydir',
      controller: function($scope){
        var thisdir = this;

        // Using ES6 arrow functions
        thisdir.shoutOut = () => {
          console.log("Shouting out!");
        }
      }
   }
});
现在在IE11上,它抛出一个语法箭头,因为浏览器无法识别箭头函数。但是,我仍然可以使用:

function shoutOut(){
   console.log("Shouting out!");
}
但我希望能够在整个指令中使用
var thisdir=this
。为了代码的干净性,我还希望避免在指令中使用
$scope
。最终,为了创建更大的指令/组件,我的指令将相互依赖,这就是为什么我想使用
var thisdir=this

那么,如何在保持与旧浏览器的语法兼容性的同时实现这一点呢?如果你看到一个更干净的方法,请让我知道

更换

thisdir.shoutOut = () => {
    console.log("Shouting out!");
}
按照ES5标准:

thisdir.shoutOut = function() {
    console.log("Shouting out!");
};

或者使用Babel之类的transpiler将您的ES6代码转换为ES5代码。

太棒了,这正是我想要的!我没意识到事情有那么简单。