cscan
cscan

Reputation: 3840

Parameter not Resolving in Angular Modal

I'm creating a simple update modal. To do so, I'm passing a parameter into my angular-bootstrap modal. I've two controllers, ModalCtrl and ModalInstanceCtrl:

adminController.controller('ModalCtrl', ['$scope', '$modal', '$log', 'Profile',
        function ($scope, $modal, $log, Profile) {
    $scope.open = function (profile) {
        var modalInstance = $modal.open({
            animation: true,
            templateUrl: 'myModalContent.html',
            controller: 'ModalInstanceCtrl',
            resolve: {
                profile: function () {
                    $log.info(profile);
                    return profile;
                }
            }
        });
        modalInstance.result.then(function (updatedProfile) {
            $log.info(updatedProfile);
            Profile.post(updatedProfile);
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
}]);
adminController.controller('ModalInstanceCtrl', function ($scope, $modalInstance, profile) {
    $scope.ok = function () {
        $modalInstance.close(profile);
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});

The profile is logged both when opening and accepting the modal - leading me to believe that it's being passed into the instance without issue. However, displaying the modal doesn't display anything from that profile object:

<script type="text/ng-template" id="myModalContent.html">
    <div class="modal-header">
        <h3 class="modal-title">Profile View</h3>
    </div>
    <div class="modal-body">
        <div><p>{{profile.profileType}}</p></div>
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" type="button" ng-click="ok()">OK</button>
        <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
    </div>
</script>

What am I doing wrong?

Upvotes: 0

Views: 62

Answers (1)

Sunil D.
Sunil D.

Reputation: 18193

You need to put the profile object on the $scope for it to be accessible in the HTML template:

adminController.controller('ModalInstanceCtrl', function ($scope, $modalInstance, profile) {

    $scope.profile = profile;

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

Upvotes: 1

Related Questions