1

I have tried writing angularJS controller inside experimental javascript of variation in optimizely. it does not seems to work.

Basically i would like to insert HTML snippet like this

<div ng-app="myApp">
        <div ng-controller="MainController">firstApp</div>
</div>

and an controller for this like below:

var myApp = angular.module("myApp", []);        
myApp.controller("MainController", function($scope, $window) {
    alert("Angularjs call function on page load");
});

i have tried few ways and its not working, Is there any way to make it work

Plnkr code: Demo

n92
  • 7,424
  • 27
  • 93
  • 129

1 Answers1

0

Your html should be

<div ng-app="testApp">
    <div ng-controller="MainController">firstApp</div>
</div>

and js

    var myApp = angular.module("testApp", []);        
myApp.controller("MainController", function($scope, $window) {
    alert("Angularjs call function on page load");
});

Module app name should be same to ng-app and value of ng-controller is the name of the controller you will use

vaibhavmaster
  • 659
  • 6
  • 10
  • that was a typo in my post, i have module name properly, this is not the actual problem. – n92 Jul 13 '17 at 12:48
  • 1
    Ok, Are you adding the angular library? Also, Can you check console and post the error here?. – vaibhavmaster Jul 13 '17 at 12:53
  • i have tried manual angular bootstrap, it says its already been done and i have tried embedding the script directly using HTML insert – n92 Jul 13 '17 at 13:03
  • No need for manual bootstrap, your code is ok and it's working (if it's not a typo error) when I tried it on plunker. You are missing something very small – vaibhavmaster Jul 13 '17 at 13:12
  • Oops controller was missing in my post but i have working code https://plnkr.co/edit/owonSOoCyd9EsaFPTj1Y?p=preview. It does not work in optimizely – n92 Jul 13 '17 at 13:19
  • Did you add the angular library using [loadscript](https://developers.optimizely.com/classic/javascript/code-samples/index.html#helpers-external-js)? You should put your following variation code in the callback, and you can edit the code to include `fileRef.setAttribute('data-semver', '1.6.2');`, etc. – Joel Balmer Jul 13 '17 at 16:32