相关疑难解决方法(0)

哪些浏览器支持bind()?

我必须在下面的方法中使用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)

javascript

11
推荐指数
1
解决办法
6211
查看次数

AngularJS使用bind(this)

我最近转而在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)

我理解为什么我必须这样做(" 这个 "上下文不断变化),有什么更好的解决方案(清洁,可用)我应该考虑做什么?

谢谢.

html javascript angularjs

2
推荐指数
1
解决办法
4573
查看次数

标签 统计

javascript ×2

angularjs ×1

html ×1