2

I am using ng-repeat on an element like this:

<div ng-repeat="aSize in BC.aOutputSizesArr" style="width:{{aSize}}px; height:{{aSize}}px;">
    {{aSize}}
    <canvas/>
    <script>alert({{aSize}})</script>
</div>

So basically on every repeat, i need to draw to the canvas based on the value of aSize, is it possible to execute a function on every iteration of ng-repeat? I tried putting that script tag in there, but it doesnt work.

3
  • 1
    make it a directive like <my-canvas aSzie></my-canvas>? Commented Sep 9, 2015 at 1:52
  • Thanks @jorg but what i mean to do is I want to run a function every time, for example it should alert the size on every time something is added. Commented Sep 9, 2015 at 1:57
  • Yes, but you execute a function in a directive. That's what they're for... Commented Sep 9, 2015 at 1:58

4 Answers 4

5

Here's an example of what I mean with using a directive.

This directive:

angular.module('directives', []).directive('alerter', function () {
    return {
        model: {
            size: '@'
        },
        link: function ($scope, element, attrs, controller) {
            alert(attrs.size)
        }
    };
});

Used like:

<alerter size=10>alert 10</alerter>
<alerter size=15>alert 15</alerter>

Will execute.

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

1 Comment

Holy hell that is amazing! Thank you jorg!
3

You can use a custom directive or the directive ngInit and pass a function from the controller.

This directive will execute once the tag is created by the ngRepeat.

<canva ng-init="function()"/> <!-- function from $scope -->

3 Comments

Ah nginit is much easier thank you so much alvaro! Is there anyway to refer to the element from that function? Like my goal is to draw to that canvas, I cant seem to do this.width to get canvas.width
Hey @Alvaro do you know how to access the element from within the ng-init? Like I want to get and set some attributes on that html element.
you can pass an $index value and make it the id. will edit the answer
1

As @Jorg said, create a directive:

.directive('myCanvas', function(){
    return {
        scope: {
            size: '=size'
        },
        template: '<canvas></canvas>',
        link: function(scope, elem, attrs){
            alert(scope.size);
        }
    };
});

Then inside your ng-repeat

<div ng-repeat="aSize in BC.aOutputSizesArr">
    {{aSize}}
    <my-canvas size="aSize"/>
</div>

This was quickly written and untested, but hopefully you get the idea. Just remember that the example above is just one way of binding, depending on your requirements for aSize (like can it be changed dynamically, etc).

Comments

0
<tr ng-repeat="app in appList" ng-init="getActivationFunction(app)"> <!-- function from $scope -->
    <td><h4> {{ app.Name }} </h4></td>
    <td> <img src="{{ app.ava_img }}"/> </td>
</tr>

in controller call the below function ...

$scope.getActivationFunction = function(modelRecieve) {
  Service.getServiceDate(modelRecieve.name)
    .then(function(response) {
       var date = response.data.image;  
       $scope.app.ava_img = date;
     },
   // ...
};

Comments

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.