I'm trying to create a directive for on/off button-groups using ui-bootstrap where I bind the ng-model to a $scope value at runtime inside an ng-repeat. It works fine if I add the ng-model directly as an attribute value, but I can't seem to set the ng-model through a directive scope variable.
Here is the example code: http://plnkr.co/edit/RS13ncXIUngdK85AqRWV
html
// directive html markup
<!-- if I set my-ng-model='CFG.debug' in markup it works! -->
<!-- but NOT if I set my-ng-model by script -->
<on-off-switch my-ng-model=""></on-off-switch>
javascript
angular.module(
'myApp', ['ui.bootstrap'] )
.directive(
'onOffSwitch',
function() {
var link = function(scope, element, attrs) {
// $scope.CFG.debug = [true|false]
// options.switchModel = 'CFG.debug'
var ngModel;
ngModel = scope.$parent.options.switchModel;
if (ngModel !== null) {
element.attr('my-ng-model', ngModel);
console.log('ngModel should be='+ngModel)
}
};
return {
template:
'<div class="btn-group">\
<button type="button" class="btn btn-primary" ng-model="myNgModel" btn-radio="false">Off</button>\
<button type="button" class="btn btn-primary" ng-model="myNgModel" btn-radio="true">On</button>\
</div>',
restrict: 'AE',
scope: {
myNgModel: '='
},
link: link
};
})