user3713267 user3713267 - 4 months ago 43
AngularJS Question

ng-disabled with bootstrap dropdown

I'd like my textfield be disabled depending on the bootstrap dropdown value.
So I wrote this

Dropdown

<div class="dropdown">
<div ng-controller="dropDownCtrl">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">
{{selectedItem}}<span class="caret"></span>
</button>
<ul class="dropdown-menu" ng-model="dropDown">
<li ng-repeat="a in subjects"><a ng-click="dropboxitemselected(a)">{{a}}</a></li>
</ul>
</div>


textfield

<div ng-controller="dropDownCtrl as ddc">
<label for="requester">Requester</label>
<input type="text" class="form-control" ng-disabled="ddc.checker" ng-model="requester" id="requester" placeholder="requester" />
</div>


and here's angular controller

app.controller('dropDownCtrl', function ($scope) {

$scope.subjects = ['Yes','No'];
this.checker=false;
$scope.selectedItem;

$scope.dropboxitemselected = function (item) {
$scope.selectedItem = item;
if($scope.selectedItem == "Yes") {
this.checker=true;
alert($scope.selectedItem);
}
}
});


All of the above are in the same tag. Alert tells me that function is called however textfield is still enabled. Can somebody tell my why ? Thanks!

Answer

Each controller has its own scope, and each call to ng-controller, you get a new instance of the controller.

So the checker variable does not get shared between the two parts where you're specifying the controller.

You may test to see what I mean with this jsfiddle