ngplayground
ngplayground

Reputation: 21667

passing the value in angular from a modal

I have a modal window in Angular which opens when a button is pressed.

When the modal is open I have a list of buttons with images inside them. The example of this is below.

I am trying to detect when ng-click="chosen(pink)" happens so that it writes to the main apps $scopewith the value pink but when I run the below it writes to the console.log undefined

angular

    $scope.choosepin = function () {
        var modalInstance = $modal.open({
            templateUrl: 'template/modal-choose-pin.html',
            controller: ModalInstanceCtrl3,
            resolve: {},
            scope: $scope.$new()
        });
        modalInstance.result.then(function (selectedItem) {
            $scope.user.pin = selectedItem;
            console.log($scope);
        });

    };

ModalInstanceCtrl3 = function ($scope, $modalInstance) {
        $scope.input = [];

        $scope.ok = function () {
            $modalInstance.close($scope.pin);
            $scope.gps = "";
            $scope.title = "";
        };

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

        $scope.chosen = function(index) {
            console.log(index);
        }
    };

html

<button type="button" ng-click="chosen(pink)"><img src="/mapbuilder/assets/img/markers/pin-pink.png" /></button>

Upvotes: 0

Views: 91

Answers (1)

Poyraz Yilmaz
Poyraz Yilmaz

Reputation: 5857

ng-click directive expecting expression and because of that it looks for a $scope variable with name pink, and of course it is undefined because you do not have such variable in your controller.

if you want to send String as a parameter you should do it like this,

ng-click="chosen('pink')"

Upvotes: 0

Related Questions