Unable to update the `$scope` index value from DOM click

3gwebtrain

In my directive, i have custom templates and replacing with existing one. when the custom templates click, i need to update the $scope.index to update.

but it's not working. but when i console the property it all works. what is the correct way to do this?

here is my custom directive:

var myApp = angular.module('myApp', []);

myApp.controller('main', function ($scope) {

  $scope.values = [{"name":"one", "num" : 1}, {"name":"two", "num" : 2}, {"name":"three", "num" : 3}]

  $scope.index = 0;

  $scope.update = function (num) {
    $scope.index = num;
  }

});

myApp.directive("newArray", function ($compile) {

  return {

    scope : {
      value : "=",
      index : "=",
      update:"&"
    },

    link : function (scope, element, attrs) {

      var getTemplate = function (val, index) {

        switch(index) {

          case 0 :
            return $('<div />', {
              class:'red',
               html : "<h1>testing</h1>",
              click : function () {
                console.log(scope.value.num); //works
                scope.update(scope.value.num); //not wroking
              }
            });
            break;

            case 1 :
            return $('<div />', {
              class:'blue',
              html : "<h1>testing</h1>",
              click : function () {
                scope.update(scope.value.num);
              }
            });
            break;

            case 2 :
            return $('<div />', {
              class:'green',
              html : "<h1>testing</h1>",
              click : function () {
                scope.update(scope.value.num);
              }
            });
            break;

        }

      }

      element.html(getTemplate(scope.value, scope.index));
      $compile(element.contents())(scope);
      element.replaceWith(element.contents());

    }

  }

})

Live Demo

zszep

In the html change the line

<new-array index='$index' update="update" value='value' ng-repeat="value in values">{{value.name}}</new-array>

to

<new-array index='$index' update="update($index)" value='value' ng-repeat="value in values">{{value.name}}</new-array>

In the js change:

scope.update(scope.value.num);

to

scope.update({num: scope.value.num});

and finally change:

$scope.update = function (num) {
    $scope.index = num;
  }

to

 $scope.update = function (num) {
    $scope.index = num;
    $scope.$apply();
  }

See updated plunker

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unable to get value from scope using directive

From Dev

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

From Dev

How to update the scope value using the custom element click event?

From Dev

How to update the scope value using the custom element click event?

From Dev

angular ng-click inside ng-repeat to update $scope and then use $apply to update dom

From Dev

Update the a scope value after the $index hit a certain number

From Dev

Unable to update $scope after executing GET call from $http

From Dev

Update URL with value from input on click with React

From Dev

Update a model value and update DOM from button in ng-repeat

From Dev

How to update the value from todo list : when click on update button value should get edited on text field and should get updated to same index

From Dev

Update value in array with value of same index from another array

From Dev

Use a directive to display scope value and update scope

From Dev

Unable to get property 'value' from search bar after click

From Dev

CSS Transition delaying DOM/scope update in AngularJs

From Dev

Unable to update a value of a hash

From Dev

Unable to update a value of a hash

From Dev

Unable to update database column value from rails console

From Dev

Unable to update database column value from rails console

From Dev

Update scope value after dynamically update textbox

From Dev

Update controller scope from directive

From Dev

Angular update scope from event

From Dev

Angular update scope from event

From Dev

set scope-value on click in angular?

From Dev

Change scope value on ng-click

From Dev

Change scope value on ng-click

From Dev

update value on button single click

From Dev

update value on button single click

From Dev

Update value on Recyclerview click listener

From Dev

AngularJs scope - how to get scope from DOM correctly?

Related Related

  1. 1

    Unable to get value from scope using directive

  2. 2

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

  3. 3

    How to update the scope value using the custom element click event?

  4. 4

    How to update the scope value using the custom element click event?

  5. 5

    angular ng-click inside ng-repeat to update $scope and then use $apply to update dom

  6. 6

    Update the a scope value after the $index hit a certain number

  7. 7

    Unable to update $scope after executing GET call from $http

  8. 8

    Update URL with value from input on click with React

  9. 9

    Update a model value and update DOM from button in ng-repeat

  10. 10

    How to update the value from todo list : when click on update button value should get edited on text field and should get updated to same index

  11. 11

    Update value in array with value of same index from another array

  12. 12

    Use a directive to display scope value and update scope

  13. 13

    Unable to get property 'value' from search bar after click

  14. 14

    CSS Transition delaying DOM/scope update in AngularJs

  15. 15

    Unable to update a value of a hash

  16. 16

    Unable to update a value of a hash

  17. 17

    Unable to update database column value from rails console

  18. 18

    Unable to update database column value from rails console

  19. 19

    Update scope value after dynamically update textbox

  20. 20

    Update controller scope from directive

  21. 21

    Angular update scope from event

  22. 22

    Angular update scope from event

  23. 23

    set scope-value on click in angular?

  24. 24

    Change scope value on ng-click

  25. 25

    Change scope value on ng-click

  26. 26

    update value on button single click

  27. 27

    update value on button single click

  28. 28

    Update value on Recyclerview click listener

  29. 29

    AngularJs scope - how to get scope from DOM correctly?

HotTag

Archive