1

i need to call angular function (=controller) in JS fuction on .jsp . I had try lot of possiblities but nothing run.

I have .jsp file with my HTML and JS, and i have angular function in app.js.

/* 
** app.js
*/

$scope.addEvent = function(element, byTypeAdd) {
    	if ($scope.element && $scope.byTypeAdd){
    		$scope.events.push({ name : $scope.element, type : $scope.byTypeAdd });
    		$scope.element = '';
    	};
    }
<script>
  function fewValues() {
    var valueId = 1;
    var valueType = "Type 1";
    
    
    /*Here i want to use addEvent*/
    
  }
    

7
  • what is it you are trying to do exactly? why would you want to call a controller? can we see some code Commented Nov 3, 2014 at 8:59
  • place the app.js file before your js function. Commented Nov 3, 2014 at 9:09
  • 1
    Follow up question: why does my view not get updated. No really, don't do stuff like that. Use a service to bind your controllers or code in general together. Commented Nov 3, 2014 at 9:10
  • I don't want really call a controller but this function. All angular between div run in my .jsp but this script is under all div. Commented Nov 3, 2014 at 9:11
  • What I want to say is that if you call your angular-code from the outside nothing will work as expected. Because angular will not recognize that it should check for changes. If you go this path, you will run into other problems. So I'd advice to against even beginning to go in that direction. Commented Nov 3, 2014 at 9:18

2 Answers 2

0

I would recommend you make your addEvent method a service and do the same thing through angular.injector

Following is the example from above document

var $div = $('<div ng-controller="MyCtrl">{{content.label}}</div>');
$(document.body).append($div);

angular.element(document).injector().invoke(function($compile) {
  var scope = angular.element($div).scope();
  $compile($div)(scope);
});
Sign up to request clarification or add additional context in comments.

Comments

0

I don't find a good solution for my problem, so i am switch to JQuery and it run.

Thanks for your help

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.