1

I have a directive and a controller. The directive as fallows:

calcP.directive('modalDialog', function() {
    return {
        restrict: 'E',
        scope: {
            show: '='
        },
        replace: true, 
        transclude: true, 
        link: function(scope, element, attrs) {
            scope.dialogStyle = {};
            if (attrs.width)
                scope.dialogStyle.width = attrs.width;
            if (attrs.height)
                scope.dialogStyle.height = attrs.height;
            **scope.hideModal = function() {
                scope.show = false;
                delete $sope.types.individual;**
            };
        },
        template: "..."
    };
});

My controller:

    calcP.controller('calcPCtrl', function($scope, $http, $window, emailSenderEndpoint) {

$scope.getVariantDomovoy = function () {
        $scope.types.domovoy = $scope.variants.domovoy;
    };
    $scope.getVariantIndividual = function () {
        $scope.types.individual = $scope.variants.individual;
    };

    ...
    $scope.modalShown = false;
        $scope.toggleModal = function() {
            $scope.modalShown = !$scope.modalShown;
        };

    });

My template:

template: "<div class='ng-modal' ng-show='show'><div class='ng-modal-overlay' ng-click='hideModal()'></div><div class='ng-modal-dialog' ng-style='dialogStyle'><div class='ng-modal-close' ng-click='hideModal()'>X</div><div class='ng-modal-dialog-content' ng-transclude></div></div></div>"

I'd like to delete some $scope by adding it to a function. But the browser shows the error that it can't find the variable $scope.types.individual.

I just try to learnAangularJS by myself and still have some problems.

10
  • delete $sope.types.individual; ?? or delete $scope.types.individual;?? Commented Mar 2, 2017 at 11:39
  • As I understand correctly your types is on your controller? So you can do delete $scope.$parent.types.individual; But it seems you're trying to achieve something and use a non-angular way of doing so Commented Mar 2, 2017 at 11:42
  • @DilumN Yes, $scope, sorry .. but I still have the same error Commented Mar 2, 2017 at 11:42
  • 1
    And where does $scope come from? Commented Mar 2, 2017 at 11:43
  • 1
    So look to @DilumN answer! he 's right! Commented Mar 2, 2017 at 12:09

1 Answer 1

2

If you want to change a value of your controller from a directive, first you have to pass that variable to the directive with two way binding. Then you can change that value as below.

calcP.directive('modalDialog', function() {
    return {
        restrict: 'E',
        scope: {
            show: '=',
            types: '='
        },
        replace: true, 
        transclude: true, 
        link: function(scope, element, attrs) {
            scope.dialogStyle = {};
            if (attrs.width)
                scope.dialogStyle.width = attrs.width;
            if (attrs.height)
                scope.dialogStyle.height = attrs.height;
            **scope.hideModal = function() {
                scope.show = false;
                scope.types.individual = "";**
            };
        },
        template: "..."
    };
});

Make sure you are passing your $scope.types from the controller to the directive. Same as you pass show parameter

May be like this,

<model-dialog show="show" types="types"></model-dialog>
Sign up to request clarification or add additional context in comments.

3 Comments

I have added my template to the original post. I have the whole application inside <model-dialog>
Can you put your html code where you include <model-dialog>
Sorry, I have found my mistake. Your answer helped me. Thank you very much.

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.