Ugh I'm stuck in one of those Angular binds (no pun intended) where I can't get my controller to talk to my directive.
My directive is the following, a select dropdown with a template:
app.directive('month', function() {
return {
replace:true,
scope:{
months:"=",
monthChoice:"="
},
template:'<select ng-model="monthChoice" ng-options=\"currMonth for currMonth in months\" class=\"monthsClass\"></select>',
link: function (scope, element, attrs) {
var lastEntry = scope.months.length - 1;
scope.monthChoice = scope.months[lastEntry];
scope.$watch('monthChoice', function() {
console.log(scope.monthChoice);
});
}
}
})
The months
values that populate the select are coming from a service that communicates to the controller:
app.controller('CandListCtrl', ['$scope', 'Months',
function ($scope, Months) {
$scope.months = Months.init();
$scope.$watch('monthChoice', function() {
console.log($scope.monthChoice);
});
$scope.whichMonth = function(m) {
console.log(m);
console.log($scope.month);
return true
}
}]);
What I would like to be able to do is to pass the value of the model monthChoice
to the controller when a change occurs. That way, I can access it from other html elements in my partial view. My partial view is set up as follows:
<month months="months" ng-change="whichMonth(monthChoice)"></month><br>
It is inside a partial that is routed using a typical $routeProvider:
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'partials/cand-list.html',
controller: 'CandListCtrl'
}).
otherwise({
redirectTo: '/'
});
}]);
I am throwing the following error: Expression 'undefined' used with directive 'month' is non-assignable!
And I am unable to access the value from the controller.