javascript - AngularJS access ngrepeat child scope variable in directive -


i have 2 controls displayed in each ng-repeat, 1 dropdown box select address type, , second input custom directive.

i want access selected value in first control (set in variable aflag) in directive of second control. think aflag child scope variable can't retrieve child scope in function toggleaflag() set flag how access flag in directive.

below html code

<div ng-app="myapp">   <div ng-controller="appctrl">     <div ng-repeat="item in itemlist">         <div>                                <select ng-options="addresstype.name addresstype in item.addresstypes" ng-model="addresstype"               ng-change="toggleaflag(addresstype)" >             </select>         </div>         <div> <input type="text" ng-model="value" my-directive></input></div>     </div>   </div>     </div> 

javascript

function appctrl(){     $scope.toggleaflag = function(addresstype) {     if (addresstype == 'business')         $scope.aflag = true;     else {         $scope.aflag = false;     }   }; }  var myapp = angular.module('myapp',[]);  myapp.directive('mydirective', function(){     return {         require: '?ngmodel',         link: function(scope, element, attrs, model){             if (scope.aflag == true){                 //do on element variable             }         }     }; }); 

i assume data model looks this

$scope.itemlist = [{     addresstypes: [{         name: 'add1',         addresstype: 'business'     }, {         name: 'add2',         addresstype: 'non business'     }] }]; 

make following changes:

ng-change="toggleaflag(addresstype.addresstype)"   //pass in addresstype value of current obj  <input type="text" ng-model="aflag" my-directive></input>  //use aflag ng-model, angularjs binds automatically  link: function (scope, element, attrs, model) {     scope.$watch(function () {  //use $watch watch change, logic triggered if there change relating scope.         if (scope.aflag == true) {             //do on element variable         }     }) } 

demo


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -