user3713267 user3713267 - 1 year ago 200
AngularJS Question

ng-disabled with bootstrap dropdown

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


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


<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" />

and here's angular controller

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

$scope.subjects = ['Yes','No'];

$scope.dropboxitemselected = function (item) {
$scope.selectedItem = item;
if($scope.selectedItem == "Yes") {

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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download