Not able to update the `scope` value from directive function

3gwebtrain

Basically, i am using a service for to manage the popup-modal. some point i made the mistake or wrongly i have understand the way of using the service, i am not able to update the scope value here.

my service.js

"use strict";
angular.module("tcpApp").service("modalService", function () {

    this.Name = "My Modal";
    this.showModal = false; //default false.

});

controller.js:

$scope.modalService.Name = "Mo!" //changing the name works!
$scope.showModal = true; //changing the ng-show works!

here is my directive :

"use strict";

var modalPopup = function ( modalService ) {

    return {

        restrict : "E",

        replace : true,

        scope : {

            showModal:"=" //getting value from controller.js

        },

        templateUrl : "views/tools/modalPopup.html",


        link : function ( scope, element, attrs ) {

            scope.Name = modalService.Name; //if i remove this Name not working!

            scope.hideModal = function () {
                alert("i am called");
                scope.showModal = false; //it's not updating the value!


            }

        }

    }

}

angular.module("tcpApp")
.directive("modalPopup", modalPopup);

here is my html in the index.html :

<modal-popup ng-show="showModal" showModal="showModal"></modal-popup>

here is my template in views/tools/modalPopup.html

<div class='ng-modal'>
    <div class='ng-modal-overlay'></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'>Please test me {{Name}}</div>
    </div>
</div>

I am clicking on the hideModal(), But the showModal is not became false and the poup-up modal not closing.

where is the mistake here? and how the way i wrongly understand the service here? or what is the correct way to do this?

Thanks in advance.

dting

You don't need to pass around anything in your view because you have a service setup to do this for you:

(function() {
  "use strict";
  var app = angular.module("tcpApp", []);

  app.controller('someController', function($scope, modalService) {
    $scope.modal = modalService;
    $scope.modal.Name = "Mo!"
  });

  app.service("modalService", function() {
    this.Name = "My Modal";
    this.isOpen = false;
    this.hide = function() {
      this.isOpen = false;
    };
    this.show = function() {
      this.isOpen = true;
    };
  });
})();

(function() {
  "use strict";

  angular.module("tcpApp").directive("modalPopup", function(modalService) {
    return {
      restrict: "E",
      replace: true,
      scope: {},
      templateUrl: "modalPopup.html",
      link: function(scope, element, attrs) {
        scope.modal = modalService;
      }
    }
  });
})();
.ng-modal {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
.ng-modal:after {
  content: "";
  display: block;
  position: absolute;
  height: 100%;
  width: 100%;
  z-index: 10;
  background-color: rgba(0, 0, 0, 0.2);
}
.ng-modal-dialog {   
  width: 300px;
  height: 150px;
  position: absolute;
  left: 50%;
  top: 15px;
  margin-left: -150px;
  z-index: 100;
  text-align: center;
  background-color: white;
}
.ng-modal-close {
  width: 32px;
  height: 32px;
  line-height: 32px;
  border-radius: 50%;
  background-color: red;
  margin: 5px auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>

<div ng-app='tcpApp'>
  <div ng-controller='someController'>
    <input type="text" ng-model="modal.Name" />
    <button ng-click='modal.show()'>show modal</button>
    <modal-popup></modal-popup>
  </div>

  <script type="text/ng-template" id="modalPopup.html">
    <div class='ng-modal' ng-if="modal.isOpen">
      <div class='ng-modal-dialog' ng-style='dialogStyle'>
        <div class='ng-modal-close' ng-click='modal.hide()'>X</div>
        <div class='ng-modal-dialog-content'>Please test me {{modal.Name}}</div>
        <input type=text" ng-model="modal.Name"/>
      </div>
    </div>
  </script>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Not able to get the value from the `directive` scope

From Dev

Not able to get the value from the `directive` scope

From Dev

Not able to get the = Local scope value inside directive

From Dev

Calling controller function from directive does not update scope

From Dev

Use a directive to display scope value and update scope

From Dev

Update controller scope from directive

From Dev

AngularJs directive, scope with value and function

From Dev

AngularJs directive, scope with value and function

From Dev

Changing a scope value from a directive

From Dev

How to update the `$scope` object from `directive` controller

From Dev

AngularJS : update parent scope from directive

From Dev

Update the Parent Scope from Child Directive

From Dev

Watch on controller scope from directive link function

From Dev

Get value from directive to $scope in controller

From Dev

Unable to get value from scope using directive

From Dev

Change controller scope value from directive AngularJS

From Dev

Not able to return the value from a function

From Dev

Use, in child directive, value from parent directive and $scope

From Dev

Use, in child directive, value from parent directive and $scope

From Dev

How to change all directive scope value from any one of directive?

From Dev

Update scope name into directive

From Dev

AngularJS : How to change scope value from isolated scope within directive

From Dev

Change a scope value in directive

From Dev

Angular.js How to update the scope from a directive?

From Dev

how to update a value from 'controller', in a 'directive'?

From Dev

AngularJs: Directive with function for template property. How to get scope value?

From Dev

AngularJs: Directive with function for template property. How to get scope value?

From Dev

Pass value from child directive isolated scope to parent directive isolated scope and trigger watch of parent

From Dev

AngularJS : Directive not able to access isolate scope objects

Related Related

  1. 1

    Not able to get the value from the `directive` scope

  2. 2

    Not able to get the value from the `directive` scope

  3. 3

    Not able to get the = Local scope value inside directive

  4. 4

    Calling controller function from directive does not update scope

  5. 5

    Use a directive to display scope value and update scope

  6. 6

    Update controller scope from directive

  7. 7

    AngularJs directive, scope with value and function

  8. 8

    AngularJs directive, scope with value and function

  9. 9

    Changing a scope value from a directive

  10. 10

    How to update the `$scope` object from `directive` controller

  11. 11

    AngularJS : update parent scope from directive

  12. 12

    Update the Parent Scope from Child Directive

  13. 13

    Watch on controller scope from directive link function

  14. 14

    Get value from directive to $scope in controller

  15. 15

    Unable to get value from scope using directive

  16. 16

    Change controller scope value from directive AngularJS

  17. 17

    Not able to return the value from a function

  18. 18

    Use, in child directive, value from parent directive and $scope

  19. 19

    Use, in child directive, value from parent directive and $scope

  20. 20

    How to change all directive scope value from any one of directive?

  21. 21

    Update scope name into directive

  22. 22

    AngularJS : How to change scope value from isolated scope within directive

  23. 23

    Change a scope value in directive

  24. 24

    Angular.js How to update the scope from a directive?

  25. 25

    how to update a value from 'controller', in a 'directive'?

  26. 26

    AngularJs: Directive with function for template property. How to get scope value?

  27. 27

    AngularJs: Directive with function for template property. How to get scope value?

  28. 28

    Pass value from child directive isolated scope to parent directive isolated scope and trigger watch of parent

  29. 29

    AngularJS : Directive not able to access isolate scope objects

HotTag

Archive