2

I have the following html and javascript code. I would like to see the script part (the alert) repeated.

<div id='ItemsContainer' ng-app='myApp'>
    <div ng-controller='Controller'>
      <div ng-repeat='item in items' >
        {{item}}
        <script>alert("{{item}}");</script>
        </div>
    </div>
</div>

function Controller($scope) {
    $scope.log = function(m) {
        console.log(m);                
    };    
    $scope.items = [1, 2];
}

Is this possible using angularjs?

3
  • You can create a custom filter and have the alert inside the filter function Commented Mar 4, 2014 at 17:26
  • you can call javascript functions using the {{ }} syntax, so you could just call a function in your controller? Perhaps this is an over- simplification of your actual need? I only say this because what you've show doesn't seem very angularjs? Commented Mar 4, 2014 at 17:32
  • It's an oversimplification indeed. What I'm trying to achieve is to repeat some javascript in an angularjs ng-repeat. Commented Mar 5, 2014 at 10:44

2 Answers 2

1

Something like this may work:

<div ng-repeat='item in items | filter: alertItem' >

function alertItem(item) { alert(item) }
Sign up to request clarification or add additional context in comments.

Comments

0

If I correct understand question, I create JSFiddle with my variant:

<div ng-app>
  <div ng-controller="MainCtrl">
      <div ng-repeat='item in items' ng-init="alertFunc(item)">
          {{item}} 
      </div>
  </div>
</div>


function MainCtrl($scope) {
    $scope.log = function(m) {
        console.log(m);                
    };    
    $scope.items = [1, 2];

    $scope.alertFunc = function(item){
        alert(item);
    }
}

1 Comment

Thanx from this angularjs newbie! this seems to satisfy my needs for now.

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.