javascriptangularjsangularjs-bootstrapangularjs-ng-init

Inject variables via ng-init to controller


I want to inject the same variables with different values multiples times to the same controller. This is what I tried. What is a way to get different values in each call?

HTML

<body ng-app="myApp">
    <div ng-controller="myCtrl" ng-init="test='helloworld';test1='helloworld2'">

    </div>
    <div ng-controller="myCtrl" ng-init="test='helloworld3';test1='helloworld4'">

    </div>
    <div ng-controller="myCtrl" ng-init="test='helloworld5';test1='helloworld6'">

    </div>
<body>

JavaScript code

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

app.controller("myCtrl", ["$scope",function($scope) {
    console.log($scope.test);
    console.log($scope.test1);
}]);

Solution

  • Found a dirty fix. Thanks all for your inputs.

    Demo

    var app = angular.module("myApp", []);
    
    app.controller("myCtrl", ["$scope", '$timeout',function($scope, $timeout) {
        $timeout(function(){ 
            console.log($scope.test);
            console.log($scope.test1);  
        }, 1);
    }]);