i create directive has transcluded content directive can bind , modify. directive has isolate scope. imagine working this:
<my-directive bound-item-name="childobj"> <input ng-model="childobj.somefield"> </my-directive> at runtime, want use childobj alias object on my-directive's isolate scope called activeobject. essentially, might think of similar way ng-repeat lets use statment obj alias in objlist , in transcluded content alias refers individual instance.
i can't seem figure out how can this... if change transluded content refer $parent.activeitem work way intended, feel that's expecting transcluded content know how directive works. seems modifying in compile function might work, except can't see, in docs, how can transcluded content. forcing transcluded content share scope directive ok, although see no evidence there's way that.
this must possible, how?
fiddling around more, able work modifying scope.$$childhead[scope.bounditemname] instead of using scope.activeobject in directive. while works i'd not rely on undocumented internal objects, if possible.
the link function of directive given transclude function 5th parameter.
link: function(scope, element, attrs, ctrls, transclude){ // ... } this transclude function takes scope variable can create , function - called "clone linking function" - places pre-linked transcluded content in dom. transclude function links against scope variable provided.
here's how works.
transclude: true, scope: {}, // free use whatever scope need link: function(scope, element, attrs, ctrls, transclude){ var boundobj = {}; // object var alias = attrs.bounditemname; // let's create isolate scope transcluded content var newscope = scope.$new(true); newscope[alias] = boundobj; transclude(newscope, function(prelinkcontent){ element.append(prelinkcontent); }); } then, if used example:
<my-directive bound-item-name="foo"> <input ng-model="foo.text"> </my-directive> then, transcluded ng-model write internal boundobj's .text property.
Comments
Post a Comment