1

I'm using Angular UI bootstrap to create a modal

I have this template

<div class="modal-body">
Topic <input type="text" ng-model="topic" />
<div id="topic-error" class="field-validation-error" ng-show="error" ng-bind="error">

</div>
<div class="modal-footer">
    <button class="btn btn-primary" ng-click="ok()" ng-disabled="!topic">OK</button>
    <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>
</div>

Then this is the modal controller

app.controller('AddTopicCtrl', ['$scope', '$modalInstance','$http',
 function ($scope, $modalInstance,$http) {

     $scope.topic = 'initial';
     $scope.error = "";
     $scope.ok = function () {

         $http.post("/api/topics/post", { name: $scope.topic })
             .success(function(result) {
                 if (result.Status == "Ok") {

                     $modalInstance.close(result.Data.Topic);
                 } else {
                     $scope.error = result.Data.Message;
                 }

         });

     };

     $scope.cancel = function () {
         $modalInstance.dismiss('cancel');
     };

 }
]);

The problem is that $scope.topic doesn't update when I enter a value in the textbox. It shows the inital value from the model but it acts only as one-way binding (model -> view). Everything else works properly.

Is there something I forgot or is there a limitation of the ng-model directive?

4
  • Have you initialized your app and have you called your controller in your DOM? Commented Jan 3, 2014 at 12:29
  • I think that the problem is not in that concrete snippet. Can you show more html? Commented Jan 3, 2014 at 12:32
  • See this jsfiddle.net/2uwTL/2 - it shows the two way binding of the ng-model. Commented Jan 3, 2014 at 12:33
  • It should be in that snippet, because that the only place where it doesn't work. I'm using this example angular-ui.github.io/bootstrap/#/modal and everything else in the modal works EXCEPT the 2 ways binding. Commented Jan 3, 2014 at 12:36

1 Answer 1

6

AngularUI Bootstrap $modal service is creating a transclusion scope for its modal windows. It means that there is one more scope between your controller and your template. This is why you need to use the "famous dot" in the ng-model expression, ex.:

In the controller:

$scope.topic = {value: 'initial'};

and in the template:

<input type="text" ng-model="topic.value" />
Sign up to request clarification or add additional context in comments.

1 Comment

It works! But I don't understand how making the value an object bypasses that additional scope.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.