Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass JavaScript variable into AngualrJs ng-init

I have the following javascript variable defined and need to pass the memId value into AngularJs init function.

<script type="text/javascript">
    var memId = "bb7de28f-0f89-4f14-8575-d494203acec7";
</script>   

<div id="content-header" class="mini" ng-init="getMember(memId)">

I am getting an error : memId is not defined.

Console shows the memId value inside ng-init is not getting passed in.

How can I pass in the javascript variable into ng-init?

like image 513
Ravi Ram Avatar asked Jan 27 '14 20:01

Ravi Ram


2 Answers

You need to do it in the "angular" way by using $window:

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

app.controller('MainCtrl', ['$scope', '$window', function($scope, $window) {
  $scope.memId = $window.memId;
}]);

Demo: http://codepen.io/qwertynl/pen/jqIrK

like image 176
qwertynl Avatar answered Nov 18 '22 21:11

qwertynl


Currently Your variables are bounded to window object. You can use Angular $window to access global window object

// Your Global Variable defined outside angular
var memId = "bb7de28f-0f89-4f14-8575-d494203acec7";

//Define Module
var app = angular.module('myapp', []);

//Define Controller
app.controller('MyCtrl', ['$scope', '$window',
    function($scope, $window) {
        $scope.memId = $window.memId;
    }
]);
like image 26
Satpal Avatar answered Nov 18 '22 21:11

Satpal