1

I am new to Angular. Not sure why directive is not working below. Searched some articles. Nothing helps

angular.module('oneApp', []).controller('OneAppController', function($scope){ //Some Logic }).directive('dvReplaceText', ['$interval', '$compile', function($interval, $compile) { return { restrict: 'A', link: function(scope, element, attr) { scope.$watch(scope.data, function(value) { element.html(value); }); } } }]); 

HTML:

<body ng-app="oneApp"> <div ng-controller="OneAppController"> <input class="input-data-box" ng-model="data" dv-replace-text=""/> </div> </body> 

JSFiddle Link

1
  • aside from you not having the Fiddle set up right (change to No Wrap - in <head>)... What is this supposed to do? you have the directive assigned to an <input> element, and then are trying to change the html of the element, but <input> doesn't have an html property. Commented Jun 18, 2015 at 7:14

4 Answers 4

4

Should be:

scope.$watch('data', function(value) { ... }); 
Sign up to request clarification or add additional context in comments.

Comments

2

You should change $watch like this :

scope.$watch('data', function(value) { element.html(value); console.log(value); }); 

Demo : http://jsfiddle.net/vikashvverma/LzLe71ft/6/

Comments

1

Try like this

angular.module('oneApp', []).controller('OneAppController', function($scope){ //Some Logic }).directive('dvReplaceText', ['$interval', '$compile', function($interval, $compile) { return { restrict: 'A', link: function(scope, element, attr) { scope.$watch("data", function(value) { //element.value(value); console.log(value); }); } } }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body ng-app="oneApp"> <div ng-controller="OneAppController"> <input class="input-data-box" ng-model="data" dv-replace-text=""/> </div> </body>

Comments

1
var app=angular.module('myApp', []); app.controller('OneAppController', function($scope){ //Some Logic console.log("data loaded"); }); app.directive('dvReplaceText', function() { return { link: function(scope, element, attr) { scope.$watch(scope.data, function(value) { element.html(value); }); } } }); 

HTML

<div ng-controller="OneAppController"> <input class="input-data-box" ng-model="data" dv-replace-text/> </div> 

Here is the working model JSFIDDLE LINK

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.