我试图为从服务填充的下拉列表编写一个简单的指令:
<select class="form-control"
name="model"
id="modelInput"
ng-model="campaign.model"
ng-change="changed()">
<option>
"--select model--"
</option>
<option ng-repeat="existingModel in allModels" value="{{existingModel}}">
{{existingModel}}
</option>
</select>
(function () {
'use strict';
var module = angular.module('components.widgets', ['rest.services']);
module.directive('modelNameSelector', function ($http, $rootScope, modelNames) {
return {
restrict: 'E',
templateUrl: 'app/components/model/model-name-selector.html',
controller: function ($scope, modelNames) {
var t = 'thing'
modelNames.getAll().$promise.then(function(data){
//why can't I see 't'???
//why can't I access $scope?
});
$scope.changed = function(){
//I can't see it here too
}
}
}
});
})();
modelNames.getAll()
与资源进行对话,并返回正确的数据,但是当它返回时我需要做一些事情,我无法访问范围或在回调闭包之外定义的任何内容。在更改后的回调中,我可以使用this
访问范围,但在getAll
的回调中,this
是窗口。我真的很困惑。