Execute script/function on each iteration of ng-repeat

后端 未结 4 2135
温柔的废话
温柔的废话 2021-01-15 14:25

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

{{aS
相关标签:
4条回答
  • 2021-01-15 15:03

    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.

    0 讨论(0)
  • 2021-01-15 15:15

    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 -->
    
    0 讨论(0)
  • 2021-01-15 15:17

    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).

    0 讨论(0)
  • 2021-01-15 15:19
    <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;
         },
       // ...
    };
    
    0 讨论(0)
提交回复
热议问题