In my application, I came across one issue and I abstracted it to the simplest case as following.
<body ng-controller="MainCtrl">
<input type="text" ng-model="color" placeholder="Enter a color"/>
<input type="text" ng-model="name" placeholder="Enter a name"/>
<hello-world/>
</body>
app.directive('helloWorld',function(){
return{
restrict: 'AE',
replace: true,
template: '<p style="background-color:{{color}}">Hello World</p>',
link: function(scope,elem,attrs){
elem.bind('click',function(){
elem.css('background-color','white');
scope.$apply(function(){
scope.color="white";
});
});
elem.bind('mouseover',function(){
elem.css('cursor','pointer');
console.log(scope.name);
});
}
}
});
In the above case, I can get the name and color property from the MainCtrl controller. The demo is here: http://plnkr.co/edit/yrljsddgTtkAo0sZVjgf?p=preview
But in my real project, my usage about controller is a little different as following:
<body ng-controller="MainCtrl as main">
<input type="text" ng-model="main.color" placeholder="Enter a color"/>
<input type="text" ng-model="main.name" placeholder="Enter a name"/>
<hello-world/>
</body>
I usually use as method as shown above.
But in this case, I don't know how to access the property in link function via scope arguments.The demo is here http://plnkr.co/edit/wnnWyRztZTounSyemPze?p=preview
Currently, the scope.name is undefined.
So any help?