I'm trying to write a simple directive for a dropdown populated from a service:
<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()
talks to a resource, and comes back with the right data, but I need to do some stuff when it comes back and I can't access scope or anything defined outside the callback closure. Inside the changed callback, I can access the scope with this
, but inside the callback from getAll
, this
is the window. I'm really confused.