【发布时间】:2015-06-29 11:30:59
【问题描述】:
这可能是一个愚蠢的问题,但是在以下情况下我可以使用什么模式来使用 bind 方法来将 this 的值绑定在下面代码中的方法中.
'use strict';
exports.service = function() {
this.colors = [];
this.years = [];
this.trims = [];
var scope = this;
this.setColors = function(colorsArr) {
scope.colors = colorsArr;
};
this.setYears = function(yearsArr) {
scope.years = yearsArr;
};
this.getVehicleDetails = function() {
return {
colors: scope.colors,
years: scope.years
};
};
this.getTrims = function() {
return trims;
};
};
【问题讨论】:
-
在我看来,你对变量所做的事情看起来更干净,假设一切正常并且你只是想使用
bind出于某种原因?另请注意,this取决于调用函数的方式,而不是定义。
标签: javascript angularjs binding