我必须在下面的方法中使用bind().我只想支持IE10及以上版本.任何人都可以验证IE10何时或是否支持bind()?
// Verify console exists
...
if (window.console) {
logger = window.console.log.bind(window.console);
// bind needed for Safari but not FF, possible IE issue?
} else {
return false; // window.console not available, silent fail
}
...
Run Code Online (Sandbox Code Playgroud) 我最近转而在ngRoute和Directives 中的控制器和控制器中使用" this " ,而不是直接使用$ scope.虽然我真的很喜欢代码的外观,但我必须将"this"绑定到每个函数 - 手动.
例:
app.controller('mainController', function ($scope, Restangular) {
this.title = '';
$scope.$on('changeTitle', function (event, data) {
this.title = data;
}.bind(this)); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
});
Run Code Online (Sandbox Code Playgroud)
我理解为什么我必须这样做(" 这个 "上下文不断变化),有什么更好的解决方案(清洁,可用)我应该考虑做什么?
谢谢.