angularjs - pass data from directive to controller -


how possible bind variable inside controller ng-model of input tag inside directive in angular?

here code im working on not working: inside directive html:

<input kendo-auto-complete k-data-source="data" ng-model="myvalue"  style="width: 100%;" /> 

my directive script:

 app.directive('myautocomplete', function () {     return {         restrict: 'e',         scope: {             data: '=info',            }         templateurl: '/directive.html',         transclude: true,         link: link,         controller:function() {             var = 4;         }     }; 

calling directive somewhere else:

    <my-autocomplete info="vm.people"  ng-hide="dialogishidden"></my-autocomplete>{{myvalue}} 

you have errors in code you've showed, example missing comma after scope property , seem referencing function link either haven't written or you're hiding us.

it looks trying use isolate scope share data between controller , directive have data two-way bound between directive , controller. think way go haven't quite implemented correctly due of errors above.

here's simplified demo of (i think) trying achieve.

demo

app.js

var app = angular.module('plunker', []);  app.controller('mainctrl', function($scope) {    var vm = this;    vm.somedata = {     value: 'bar'   };  });   app.directive('myautocomplete', [function(){      return {          restrict: 'e',          scope: {             somedata: '=',         },          templateurl: 'directive.html',       };  }]); 

index.html

<!doctype html> <html ng-app="plunker">    <head>     <meta charset="utf-8" />     <title>angularjs plunker</title>     <script>document.write('<base href="' + document.location + '" />');</script>     <link rel="stylesheet" href="style.css" />     <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.17/angular.js" data-semver="1.3.17"></script>     <script src="app.js"></script>   </head>    <body ng-controller="mainctrl vm">     <pre>{{vm.somedata}}</pre>      <my-autocomplete some-data='vm.somedata'></my-autocomplete>   </body>  </html> 

directive.html

<input    type="text"   ng-model="somedata.value"   /> 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -