0

I have a directive in angular that resembles this:

.directive('forexample', function() {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {
        elm.on('click', function() {
            scope.$apply(function () {
            ctrl.$setViewValue('value');

Called from something like:

<button forexample ng-model="mymodel">Do It</button>

So obviously

$scope.mymodel; // equals value

What I want is to push('value'); to the model from the directive so in the end after clicking "Do It" a few times, you'd get:

$scope.mymodel; // equals array('value,'value','value');

2 Answers 2

1

Given $scope.mymodel is already defined as Array, then following should work.

app.directive("forexample", function() {
  return function( scope, elm, attrs ) {
    elm.bind("click", function( evt ) {
      scope.$apply(function( scope ) {
        scope[ attrs.ngModel ].push('value');
      });
    });
  };
});
Sign up to request clarification or add additional context in comments.

2 Comments

question, if scope[ attrs.ngModel ]; can be used what is the advantage of using ctrl.$setViewValue in a directive?
To be honest, I've never used ctrl.$setViewValue and I do not know for sure. (But I guess it can be used if the isolated scope cases.)
1

The ngModel controller is usually used with input-type directives where the 2-way data binding shows its full power. But judging from your example the full machinery of the ngModel might not be required in your case. Your example doesn't explain what you are trying to do, functionally speaking, so I'm just assuming that you want to push value to an array in response to click events. If so, the easiest way of approaching this is by using the $eval method on a scope:

.directive('forexample', function() {
    return {
      link: function(scope, elm, attrs, ctrl) {
        var modelArray = scope.$eval(attrs.forexample);

        elm.bind('click', function() {            
            scope.$apply(function () {
              modelArray.push('value');
            });
        });
      }
    };
  });

The above directive can be used in a template like so:

<button forexample="somearray">Do It</button>

And here is the working plunk: http://plnkr.co/edit/xc5mui9xbxIWHxcvAjqR?p=preview

1 Comment

I'm actually accessing a phonegap camera, but I didn't want to include all of the extra code. Basically, the camera returns the fileURI that I then upload with phonegap all in the same directive. My plan is to set a status in the directive after the camera returns the fileURI (so users see it's uploading), I'm going to upload it, then I'm going to set the status to finished and push() the result (file_id and url) to the model and eventually save it again as part of a larger content type.

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.