Change controller scope value in AngularJS directive

I have a condition in which I have input fields in the listing grid, and I have one directive, now I want to send the value to this directive, whatever the input value ... up to this point it works fine, but now, when I try to change the value from the input field of the directive, it does not update the input field of the list grid for which the value for the directive is set.

Here is a working plnkr, let me know what I'm doing wrong.

http://plnkr.co/edit/DZdN4itTNccVsuBEJahr?p=preview

My controllerand directivethe code looks like -

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

myApp.controller('mainCtrl', function(){
  var vm = this;

  vm.fordirective = '';

  vm.list = [
    {id: "1", name: 'Test 1', age: 35},
    {id: "2", name: 'Test 2', age: 36},
    {id: "3", name: 'Test 3', age: 37},
    {id: "4", name: 'Test 4', age: 38},
  ];
})

myApp.directive('testdir', function(){
  return {
    restrict: 'EA',
    scope: {
      directivevalue: "="
    },
    templateUrl: 'dirtemplate.html',
    link: function(scope, elem, attrs) {

    }
  };
})
+4
source share
3 answers

vm.fordirective item:

testdir - , item . , helper:

<testdir directivevalue="vm.fordirective" field="age">Loading..</testdir>

, , directivevalue[field]:

<input type="text" ng-model="directivevalue[field]" />

: http://plnkr.co/edit/jWKwDJvYOutcLihi7UyC?p=preview

+2

plnkr -

http://plnkr.co/edit/DZdN4itTNccVsuBEJahr?p=preview

-

myApp.directive('testdir', function(){
  return {
    restrict: 'EA',
    scope: {
      directivevalue: "=",
      index: "="
    },
    templateUrl: 'dirtemplate.html',
    link: function(scope, elem, attrs) {

      scope.setParentValue = function(directivevalue){
        scope.$parent.vm.list[scope.index].age = directivevalue;
      };
    }
  };
})

ng-change, -

<input ng-model="item.age" ng-click="vm.fordirective = item.age; vm.index = $index" ng-change="vm.fordirective = item.age; vm.index = $index" />

+1

$parent ( ), angular .

.

plunkr:

https://embed.plnkr.co/LyE0G0APhwC4nco1KvOw/

Directive Code:

scope.setParentValue = function(directivevalue){ scope.directivevalue = directivevalue; };

0
source

All Articles