0

I'm trying to create a directive and am running into some problems with the scope object. In creating a plunkr to display what is happening I noticed that I'm close, but not quite sure what I'm misunderstanding.

If i have a directive that looks like this:

angular.module('inputExample', [])
 .directive('test',
 function() { 
 return{
 restrict: 'E',
 link: function(scope){
 scope.testInput = 1;
 scope.$watch(
 'testInput',
 function(newTestInput){
 console.log(newTestInput);
 },
 false);
 }
 }
 });

And utilize the directive in the view like this:

 <test>
 <input data-ng-model="testInput" />
 <div>
 {{testInput}}
 </div>

the div seems to be updated correctly and there is a message logged to the console as I would expect.

If I create the directive like this though:

angular.module('inputExample', [])
 .directive('example',
 function() { 
 return{
 scope: {
 'settings': '@'
 },
 restrict: 'E',
 link: function(scope){
 scope.exampleInput = 1;
 scope.$watch(
 'exampleInput',
 function(newTestInput){
 console.log(newTestInput);
 },
 false);
 }
 }
 });
 </test>

and use it in a view like this:

 <example>
 <input data-ng-model="exampleInput" />
 <div>
 {{exampleInput}}
 </div>
 </example>

then the div seems to get updated, but there is no message logged to the console. It seems as though the $watch isn't working. Can someone please tell me what I'm doing wrong?

Here is the plunkr.

Thanks in advance for any help that can be offered.

asked Sep 29, 2014 at 19:33
3
  • Why do you set 'settings' in the directive's scope? you don't seem to use it at all. Commented Sep 29, 2014 at 19:38
  • In this example I don't, but my actual scenario is using the settings. I can update the plunkr to reflect that, but that didn't seem to matter in causing this to happen. Commented Sep 29, 2014 at 19:44
  • Ok, no issue, I was just wondering. Commented Sep 29, 2014 at 19:47

1 Answer 1

2

The problem with the example is that you're putting your HTML inside your directive tags. They should be in your template.

This code works

 angular.module('inputExample', [])
 .directive('example',
 function() { 
 return{
 scope: {
 'settings': '@'
 },
 restrict: 'E',
 template: '<input data-ng-model="exampleInput" /><div>{{exampleInput}}</div>',
 link: function(scope){
 scope.exampleInput = 1;
 scope.$watch(
 'exampleInput',
 function(newTestInput){
 console.log(newTestInput);
 },
 false);
 }
 }
 });

With the view as just

<example></example>
answered Sep 29, 2014 at 20:40
Sign up to request clarification or add additional context in comments.

4 Comments

Excellent. This works in the plunkr. In my real example, I have the template in a separate html file and it still isn't working this way. I'll try to create a plunkr like that and see if I can get it to work. Any idea why having the template in a separate html file would be any different?
No, it should be the same. Sorry for not providint that in my answer, but am not very practised with plunkr.
Isn't issue is bcz of "scope" is defined in the directive which ovveride default scope therefore need to define "exampleInput" as well in "scope"?
@DilipKumar True, if the scope isn't overridden in the example directive, then it works. But @peinearydevelopment specified a need for the isolate scope in one of the comments.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.