javascript - AngularJS: How to set an attribute to a custom directive -
i have custom directive holds <textarea>
element along other html elements in template using templateurl
. directive should allow user type indian language (using writing script devanagri , others). user select input language.
this directive used in html:<keybuddy></keybuddy>
now problem is, in main code, how should retrieve text entered in textarea
located in template.html specified templateurl
? have text available in scope of link function since used ng-model
on textarea
.
<textarea id="inputfield" placeholder="start typing....." ng-model="inputtext" rows="5"> </textarea>
in index.html
, should able this:
<keybuddy></keybuddy> <button onclick="printtext()">show text</button> <script> var printtext = function () { console.log($("keybuddy").value); } </script>
how do it? there other better way using power of angularjs? went through these posts wasn't helpful. or might not understand how use in work. how set native attribute angularjs directive?, how evaluated attributes inside custom directive
my complete code on github: keybuddy
note:
- i should able use directive anywhere in application, number of times.
- it should portable; should able use in of project or other developer should able use without
go angular :-)
you don't need use of jquery here. angular sufficient enough. can change code this:
in view:
<keybuddy model="myinputtext"></keybuddy> <button ng-click="showtext()">show text</button>
in view controller:
$scope.showtext = function() { console.log($scope.myinputtext); }
and update directive have scope
below (replace `scope: true):
scope: { inputtext: '=model' }
read more on isolating scope of directive
Comments
Post a Comment