javascript - AngularJS template vs templateURL in directive -


i have directive defined this:

app.directive('itemcomments', ['ajax', function(ajax) {         return {             restrict: 'a',             templateurl: url + 'public/pages/homepage/ajax/getcommentstpl',             controller: 'itemlibrarycontroller',             controlleras: 'itemlibraryctrl',             link: function(scope, element, attr) {                 var $element = $(element);                 scope.$watch(function(scope) {                     return $element.find('li').length > 0;                 }, function(finished) {                     if(finished) {                         autosize($element.find('textarea'));                     }                 });             }         };     }]); 

the template specified "templateurl" returns this:

... <textarea class="form-control textarea-resize" ng-keyup="commentpost($event)" ng-model="textcomment"></textarea> ... 

in itemlibrarycontroller have defined method commentpost() accessed on keyup on textarea. problem $scope.textcomment = undefined instead of value entered in textarea. if modify in html ng-model="$parent.textcomment" value textarea found in $scope.textcomment.

ps. when using "template" instead of "templateurl" in directive definition, ng-model problem disappears.

my questions are:

  1. why have use $parent.textcomment since scope of template itemlibrarycontroller?

  2. why using templateurl creates scope ng-models inside template?

  3. why, when using "template" instead of "templateurl" in directive definition, ng-model problem disappears?

  4. how can access textcomment without using $parent.textcomment?

the problem solve issue using dot rule of angularjs, object [**prototypal inheritance**][1]. need create , object , add thetextcommentin it, object like$scope.library={}thentextcommentwill placed in it. need make addscope: true` directive follow inheritance of object.

template

... <textarea class="form-control textarea-resize"     ng-keyup="commentpost($event)"     ng-model="library.textcomment"> </textarea> ... 

directive

app.directive('itemcomments', ['ajax', function(ajax) {     return {         scope: true, //<--added here         restrict: 'a',         templateurl: url + 'public/pages/homepage/ajax/getcommentstpl',         controller: 'itemlibrarycontroller',         controlleras: 'itemlibraryctrl',         link: function(scope, element, attr) {             //code here         }     }; }]); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -