1

My Html:

<test-app email="[email protected]"></test-app>

My Directive:

.directive('testApp', function () {
return {
    restrict: 'E',
    scope: {
        userEmail = '@userEmail'
    },
    templateUrl: 'Form.html'
};
})

My Form in another Page:

<label> Email </label>
<input type="text" id="email">

Issue: I need to get the parameter value , and print it out on the textbox itself.

What i have done: I research on a lot of articles which demonstrated on how to pass directive parameters to controller but they were very complicated.

What i think is the next step:

1) Pass the directive parameter to the controller and then bind the controller to the textbox and set the value of the textbox to be the parameter value.

2) Or is there another way i can do this?

I am stuck on how to proceed on.

5
  • First try to follow directive naming convension(camelCase), for example: directive name makeBold then use it in template like: <make-bold ...> </make-bold> Commented Jan 9, 2017 at 16:03
  • @AvneshShakya edited. But that isn't going to help start. Any tips to continue? Commented Jan 9, 2017 at 16:04
  • You want email value in directive, correct? so try this: scope: { userEmail: '@email'}, and in your template: <input type="text" ng-model="userEmail" id="email"> check about isolated scope in detail here Commented Jan 9, 2017 at 16:11
  • i want the email value into the directive then to the controller. As i need to print out the value into the input text box @AvneshShakya Commented Jan 9, 2017 at 16:13
  • Ya you will get, see my comment above :) Commented Jan 9, 2017 at 16:14

1 Answer 1

1

You can use attribute string binding using @

  1. In controller you initialize your userEmail variable

  2. The directive has a user-email attribute which loads value into the directive's userEmail local scope variable.

  3. Using userEmail:'@' allows for string binding from controller to the directive.

See demo below:

angular.module("app",[]).directive('application', function() {
  return {
    restrict: 'E',
    scope: {
      userEmail : '@'
    },
    templateUrl: 'Form.html'
  };
}).controller("ctrl",function($scope){
  $scope.userEmail="[email protected]";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="ctrl">
  <application user-email="{{userEmail}}"></application>
  <script type="text/ng-template" id="Form.html">
    <label>Email</label>
    <input type="text" id="email" ng-model="userEmail">
  </script>
</div>

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

4 Comments

your answer is sort of close. But i want the value in the directive which the user might enter straight in for example : <test-app email="[email protected]"></test-app> . I wan the '[email protected]' to be inserted into the form's textbox
@JohnTan well, I'm not sure what you are looking for, you can use <application user-email="[email protected]"></application> ?
@JohnTan just following up on this - so you managed to solve the issue? If this answer helped, upvote/accept too, thanks!
Hi ! sorry. i did not managed to solve this. Is it possible to do it with another way ?? i think its because of my application structure as i have a few templateUrl and it cant pass directly to the controller

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.