1

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?

1
  • the demo link was updated Commented Jan 17, 2017 at 11:06

1 Answer 1

1

As per your plunkr, You need to make it scope.main.name and scope.main.color ie

index.html

 <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>

script side

var app = angular.module('myapp', []);

app.controller('MainCtrl', function($scope) {

});

app.directive('helloWorld',function(){
  return{
    restrict: 'AE',
    replace: true,
    template: '<p style="background-color:{{main.color}}">Hello World</p>',
    link: function(scope,elem,attrs){
      elem.bind('click',function(){
        elem.css('background-color','white');
        scope.$apply(function(){
          scope.main.color="white";
        });
      });
      elem.bind('mouseover',function(){
        console.log(scope.main.name);
        console.log(scope.main.color);
      });
    }
  }
});

Check the elem.bind function for the change

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.