angularjs - How can I bind deep child property of directive's scope? -
My dear friend,
We can tie up the scope of the scope of instructions for the value of the DOM attribute.
This works:
Module Directive 'Directive Directive', - & gt; Scope: Directive: '='
...
& lt; Div class = 'midedirective' directive-ver = 'parentware' & gt; & Lt; / Div & gt;
In the above example, we can tie the director's directive
property to parentware
parent parent's property.
This is a bi-directional binding, so if directivvar
changes parent virus
is automatically updated, and vice versa.
My question is this:
Is there any way that I can tie the property of a deep child within the scope of my direction? Instead of scope.directiveVar
scope.lv1.directiveVar
or scope.lv1.lv2.lv3.directiveVar
as
< / P>
What do I want to achieve
I have an object in the directory area called lv1
I have my property
area: lv1: directiveVar: '='
Pre>
And it does not work:
scope: "lv1.directivevar": '= myvar'
Demo
this The job is:
This is what I want to achieve:
me Hope this code will help you pass an object and you can see its property or you can nest in the instructions of the parent / child. By adding "=", the two objects will be able to bind the whole object in both ways.
Administrator:
$ scope.directiveVar = {level1: {greeting: 'hello'}}; $ Scope.otherVar = {levelA: {foo: 'bar'}};
Markup:
& lt; Div data-my-parent-directive data-other-var = "otherVar" & gt; & Lt; Div data-meri-directive -directive-ve = "directivever" & gt; & Lt; / Div & gt; & Lt; / Div & gt;
Director:
Angular Module ('my directive', []). Directive ('myParent Directive', ['$ window'], function ($ window) {return {restrict: 'AE', scope: {otherVar: '='}, controller: function ($ radius, $ element) { Var othis = this; element = $ element; this.otherVar = $ scope.otherVar;}};}]). Directive ('myDirective', ['$ window', function ($ window)) {return {restricted: 'AE', required: '? MyParent Directive ', Scope: {directiveVar:' = '}, Link: Function (Scope, Elm, Entry, MyParent Directive Ctrl) {console.log (myParent DirectiveCtrl.otherVar); Console.log (myDirectiveParentCtrl.otherVar.levelA); Scope $ Watch ('directivevar.level1', function (newcomer, older) {Console.log (newValue, oldValue);}); }}; }]
Edit:
You can do just that:
& Lt; Direct Data-My-Parent-Directive Data-Other- var = "Other" & gt; & Lt; Div data-meri-directive-data-direct-ver = "directwire level 1" & gt; & Lt; / Div & gt; & Lt; / Div & gt;
Which is useful when modeling data for example:
var item = apiService.getItems (); Var layers = [...], $ scope.map = {point: items, layers: layers, high almatem: 'roads', low altim: 'satellite'};
It has been said that if you are modeling the data see this which I believe the speaker touches on some oo samples.
Edit2:
You can use a service
Comments
Post a Comment