0

In this plunk I have an Angular Modal UI with a variable that is watched, however the watch function is not triggered when the variable is changed, what's wrong?

Javascript

var app = angular.module('app', ['ui.bootstrap']);
app.controller('ctl', function ($scope,$uibModal) {

        $scope.x = 'a';

        $scope.open = function(){
          $scope.modalInstance = $uibModal.open({
              templateUrl: 'myModalContent.html',
              scope: $scope
            }); 
        };

        $scope.close = function(){
          $scope.modalInstance.close();
        };


        $scope.$watch('x', function (newValue, oldValue) {
          if (typeof newValue === 'undefined')
              return;
           alert('value='+$scope.newValue);
       });
});

HTML

<button ng-click="open()">Open</button>
<script type="text/ng-template" id="myModalContent.html">
   <div class="modal-header">
      <h4 class="modal-title">The Title</h4>
      value <input type="text" ng-model="x" />
      <button ng-click="close()">Close</button>
   </div>
</script>
2
  • Try adding your controller to your modal instance, such as $uibModal.open({ templateUrl: "myModalContent.html", scope: $scope, controller: "ctl" }) Commented Jan 9, 2018 at 13:34
  • thanks, that fixed the problem Commented Jan 9, 2018 at 13:39

3 Answers 3

1

Just tested in the plunker, $uibModal needs your controller in order to bind to your scope.

Change;

$scope.open = function(){
      $scope.modalInstance = $uibModal.open({
          templateUrl: 'myModalContent.html',
          scope: $scope
        }); 
    };

To;

$scope.open = function(){
      $scope.modalInstance = $uibModal.open({
          templateUrl: 'myModalContent.html',
          scope: $scope,
          controller: "ctl"
        }); 
    };

If you are using a .js file for it, you'd use controllerUrl = "path/to/jsfile.js" plus the name of said controller.

Sign up to request clarification or add additional context in comments.

Comments

1

Please define controller in $uibModal.open for more reference please visit this document in details https://github.com/angular-ui/bootstrap/tree/master/src/modal/docs

Comments

0

This question helped in resolving $uibModal onload event

just added in modalController

$scope.open = function () {
    $scope.status.opened = true;
};
$scope.$watch(status, function (newValue, oldValue) {
    //code required onload..
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.