0

I had already declared my app and controller in separate file and below is how i am loading my controller html DOM but it is not showing that message. Can someone please guide how to achieve this.

HTML:

<div id="bnProblemList">
    <div ng-controller="problemListCtrl" data-ng-init="init()">
        <div ng-view="">this is my message = {{message}}</div>
    </div>
</div>

JS:

html = $j('#bnProblemList').html();
$compile(html)(scope);

Please let me know how to inject or load ng-controller html dynamically.

4
  • Is your ng-app set before use the ng-controller ? Is your controller include on your index ? Commented Aug 24, 2015 at 11:47
  • yes i had set ng-app as html attribute only.. Commented Aug 24, 2015 at 11:49
  • and i am adding controller dynamically Commented Aug 24, 2015 at 11:50
  • It would be great if you can create a Plunkr for your app. Commented Aug 24, 2015 at 11:54

4 Answers 4

1

Your controller declaration is wrong. You should do it like this:

var myApp = angular.module('myApp',[]);

myApp.controller('problemListCtrl', ['$scope', 
  function($scope) {
      $scope.message = "This is my message :-)";
  }
]);
Sign up to request clarification or add additional context in comments.

2 Comments

I had added this code in my main app.js which looks like below:-
var _versionNumber = "1.0", $stateProviderRef; var moduleListToLoad = ['ui.router', 'oc.lazyLoad', 'ngRoute']; var enkiApp = angular.module("enkiApp", moduleListToLoad); enkiApp.controller('problemListCtrl', ['$scope', '$state', '$compile', function homeController($scope, $state, $compile) { $scope.message = 'HOME PAGE'; $scope.init = function () { console.log("init"); }; $scope.$apply(function(){ console.log("apply"); }); $state.go('ProblemList'); }]); angular.element(document).ready(function() { angular.bootstrap(document, ['enkiApp']); });
0

A proper way is to declare your Controller into an anonymous function, and add it to a module. Then, you have to set your ngApp.

Controller.js

Here, we will declare our controller, and create an app module. Then, we will declare ctrl as our controller into our app module.

  (function(){

  function Controller($scope) {

    $scope.name = 'toto';

  }

  angular
  .module('app', [])
  .controller('ctrl', Controller);

  })();

HTML

  <body ng-app='app' ng-controller="ctrl">

    <p>My name is {{name}}</p>

 </body>

And don't forget to include your script tag :

<script src="controller.js"></script>

Comments

0

You can try Like below Example:-

Html Page:

<!DOCTYPE html>
<html ng-app="MyApp">
<head>
 <title> Controller</title>  
 <script src="../Js/angular.min.js"></script>
 <script src="../Js/Controller.js"></script>   
</head>
<body>
  <div ng-controller="MyFirstController">
    {{ Msg }}
  </div>
</body>
</html>

ControllerJs Page:

var MyApp=angular.module("MyApp",[]);
MyApp.controller("MyFirstController",function($scope){
  $scope.Msg="Hello First Conroller";
})

Comments

0
i was able to load/ inject controller dynamically by the help of below code

<div id="bn" ng-controller="myCtrl as vm">
    <div ui-view=''></div>
</div>

<script>
    angular.element(document).injector().invoke(function($compile, $state) {
        $state.go('myCtrl');
        var scope = angular.element($j("#bn")).scope();
        $compile($j("#bn"))(scope);
    });
</script>

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.