0

I have a jQuery UI dialog, and in this pop up I have a dynamic <select> with angular and Ajax request. My dialog shows up if I click on a button, but AngularJS script is played even if dialog isn't visible.

I stopped my script with a condition : if($("#MyDialog).dialog("isOpen") ===true)"

And it works, but I don't know how to "reload" my Angular Controller to load select input when my dialog is displayed...

I tried this : MyController(angular.element($('div[ng-controller="MyController"]')).scope(), angular.element($('div[ng-controller="MyController"]')).http);

but it doesn't works... Any idea ?

Code of my controller :

function MyController($scope, $http) {
    if ($("#myDialog").dialog("isOpen") === true) {
        $http.
            success(function (data, status, headers, config) {
                $scope.select = data;
            }).
            error(function (data, status, headers, config) {
                alert("error !");
            });
    }
}
2
  • That's an anti pattern in angular.js -> $("#myDialog") , This code should probably live inside a directive. please provide a plunker Commented Feb 17, 2014 at 14:44
  • 1
    There's no point to use AngularJS if you aren't trying to enforce the practices they strive to enforce. You shouldn't access DOM elements in the controller. Commented Feb 17, 2014 at 14:44

1 Answer 1

0

Instead of accessing whether the modal is open try placing a ng-click on the open modal button or link.

<button ng-click="loadSelectData()">Open Modal</button>

<script>
    app.controller("MyController", ["$scope", "$http", function($scope, $http) {
        $scope.loadSelectData = function () {
            // fetch your select data
            // also you can check if you already have the data before going to server
        }
    }]);
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I will use a jquery ui dialog controlled by angular, so my select box will be loaded on a ng-click like you did. Thanks !

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.