呃,我被困在其中一个 Angular 绑定(不是双关语)中,我无法让我的控制器与我的指令对话。
我的指令如下,一个带有模板的选择下拉菜单:
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);
});
}
}
})
填充选择的months
值来自与控制器通信的服务:
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
}
}]);
我想做的是在monthChoice
发生变化时将模型的值传递给控制器。这样,我可以从部分视图中的其他 html 元素访问它。我的局部视图设置如下:
<month months="months" ng-change="whichMonth(monthChoice)"></month><br>
它位于使用典型 $routeProvider 路由的部分内部:
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'partials/cand-list.html',
controller: 'CandListCtrl'
}).
otherwise({
redirectTo: '/'
});
}]);
我抛出以下错误:Expression 'undefined' used with directive 'month' is non-assignable!
而且我无法从控制器访问值。