javascript - Angular transclude and scopes -
i trying generate directive click-to-edit input fields. since have variety of different types of input fields needs work with, wanted make attribute type directive transcludes input field itself.
however, problem when using scope parameter directive description (for ipdisable), things stop working should (try commenting in line 44 in jsfiddle js part). presumably, therefore scope error, have no idea begin debugging it, , appreciated.
jsfiddle: http://jsfiddle.net/hbywx/3/
html:
<input inplace type="string" ip-disable=false name="username" ng-model="uname">
js:
myapp.directive('inplace', function($compile) { var compile = function(telem,tattrib,transclude) { var whole = $('<div ng-scope></div>'); var editable = $('<div class="editable-transclude" ng-hide="static">'+ '<a ng-click="changestatic()" ng-show="!static && !disable()">'+ '<save></a></div>'); whole.append(editable).append('<span class="disabledtext" ng-show="static">{{ngmodel.$viewvalue}}</span>' + '<a ng-click="changestatic()" ng-show="static && !disable()">'+ '<edit></a>'); telem.replacewith(whole); transclude(telem,function(clone) { clone.removeattr('inplace'); editable.prepend(clone); }); return function(scope, element, attrs) { var input_element = $($(element).find('input')[0]); scope.name = input_element.name; scope.ngmodel = element.controller('ngmodel'); scope.static = true; scope.changestatic = function() { if (scope.static) { scope.static = false; } else if (!scope.ngmodel.$error[scope.name]){ scope.static = true; } }; }; }; return { transclude: 'element', scope: { disable: '&ipdisable' }, restrict: 'a', compile: compile }; });
this because you're moving input
element inside element has isolate scope, can no-longer interact scope outside of it. such, uname
have bound not on same scope 1 you're feeding ng-model
of input.
you've got couple of options - first not create isolate scope @ - can still access ipdisable
through attrs
in link function.
the other (better) solution, add ngmodel
isolate scope (scope: { disable: '&ipdisable', ngmodel:'='}
,), , update value of input using ngmodelcontroller, when input changed.
Comments
Post a Comment