I tried searching for this but I didn't quite know how to phrase the idea.. Let me try and explain. I am still trying to grasp the whole scope in directives thingy..
The code below are some variations of the same directive (it is not a lot of code,, it just seems that way)
Say.. In my controller I have this
$scope.foo = {name: "John", color: "Green"};
My directive looks something like this
app.directive("miniDirective", function () {
return {
restrict: "A",
scope: {
type: "=type",
value: "@value"
},
template:'<div>{{content}}</div>' +
'<input type="text" ng-model="content">',
link: function (scope, iElm, iAttrs) {
switch (scope.value) {
case "name":
scope.content = scope.type.name; break;
case "color":
scope.content = scope.type.color; break;
}
console.log(scope.content);
}
}
})
and I would like to use my directive like this
<div mini-directive
type="foo"
value="name">
</div>
<br/>
<div mini-directive
type="foo"
value="color">
</div>
PROBLEM 1: If I use the above directive then scope.content does not bind back to scope foo (type attribute) value.. I kind of understand why this is,, BUT I have NO idea how to make that happen...
Then I tried doing it differently.. and that is where I got stuck,,,
app.directive("miniDirective", function () {
return {
restrict: "A",
scope: {
type: "=type",
value: "@value"
},
template:'<div>{{type.XXXX}}</div>' +
'<input type="text" ng-model="type.XXXX">',
link: function (scope, iElm, iAttrs) {
// I WOULD LIKE TO CHANGE xxxx based on value attribute
// AND keep the binding working
scope.type.xxxx;
}
}
})
QUESTIONS
Is there a way to convert the value from value attribute value: "@value"
into something that can then be dynamically applied to scope.type.xxxx; where xxxx is either name or color? Can it be done without using "switch" as I did in the first example or "if else" or any condition that checks for existing values...
OR,,, in the case where I used switch,, is there a way to bind scope.content back to foo.name or foo.color DEPENDING on the value being passed in the attribute?
I am on my way to try and make a jsfiddle for this...
I would be very grateful for your help.