1

I want to have an attribute directive a bit similar to ng-model. I just want to additionally bind an input fields value to a scope variable (just in one direction input field -> scope variable). So I have just tried this directive but I can not get the directive called anyway.

script:

.directive('passivemodel', function () {
  return {
    restrict: "A",
    scope: {
        passivemodel: '='
    },
    link: function (scope, element, attrs) {
        scope.$watch('passivemodel', function(newPassivemodel, oldPassivemodel) {
            console.log("passive model", newPassivemodel);
        });
    }
  };
})

html:

<input data-passivemodel="keyword">

Edit:

Hmmm .. based on vilo20 answer I am running into a very strange behavior.

while this code is working very well: <input data-ng-model="keyword" data-passivemodel="keyword">

this one does not (note the value of passivemodel): <input data-ng-model="keyword" data-passivemodel="keyword2">. Sure I have defined the variable in the controller.

Controller:

.controller('SearchController', function($scope, $routeParams, $search) {
        $scope.search = new $search();
        $scope.keyword = "";
        $scope.keyword2 = "";
})

Edit2: here is a fiddle http://jsfiddle.net/HB7LU/12107/

1
  • Take a look at the plunker I made for you in my answer Commented Mar 24, 2015 at 9:51

3 Answers 3

2

try this:

.directive('passivemodel', function () {
        return {
            restrict: "A",
            scope: {
               passivemodel: '='
            },
            link: function (scope, element, attrs) {
                console.log("passive model", scope.passivemodel);
                $scope.$watch('passivemodel', function(newPassivemodel, oldPassivemodel) {
                //put your logic when passivemodel changed
                });
            }
        };
    })

Hope it helps

Edit: Here is a plunker http://plnkr.co/edit/T039I02ai5rBbiTAHfzv?p=preview

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

5 Comments

Yes this works! But now I have a scope limited to the directive right? How can I access the controllers scope now?
Because of '=' both scopes are linked (bind). Try to change the passivemodel from the directive - you will see that it being changed also in the parent scope.
2 min to go to accept it. but I still have a problem pls see my edit.
I am looking, can you create some jsfiddle or plunker? are you getting any errors in console?
In the plunker olny the keyword is working keyword2 is doing nothing. In my fiddle not even keyword is working when I use passivemodel
0

Use the scope attribute:

.directive('passivemodel', function () {
        return {
            restrict: "A",
            scope: {
                "passivemodel": "="
            },
            link: function (scope, element, attrs) {
                console.log("access passivemodel: ", scope.passivemodel);
            }
        };
    })

Comments

0

Finally it was as simple as that:

.directive('modelRed', [function(){
        return {
            require: 'ngModel',
            restrict: 'A',
            scope: {},
            link: function (scope, element, attrs, ngModel) {
                scope.$watch(function () {
                    return ngModel.$modelValue;
                }, function(newValue) {
                    scope.$parent[attrs.modelRed] = newValue;
                    //console.log(attrs.modelRed, newValue, scope);
                });
            }
        }
    }])

And in the html:

<p><input type="text" data-ng-model="testInput" data-model-red="redInput">{{redInput}}</p>

Of course you have to define testInput and redInput in the $scope object.

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.