Mr Myo Mr Myo - 6 months ago 41
AngularJS Question

Receive ng-init value from the controller for angular js

I put data in the ng-init then I want to receive these value for my controller.

<div ng-controller='hotelDetailCtrl' data-ng-init="mytest='hello'" class="col-sm-9 col-lg-9" >

app.controller('hotelDetailCtrl', function ($scope, $http) {
$scope.init = function(){
alert($scope.mytest);
};
});


But I can't get these value from my controller.Please help me and then explain about these case.

Answer

You are trying to call init function inside your controller, but haven't called it inside your code, I assume this is what you planned to do:

var app = angular.module('myapp', []);
app.controller('hotelDetailCtrl', function ($scope, $http) {
    $scope.init = function(){           
        alert($scope.mytest);
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myapp">
      <div ng-controller='hotelDetailCtrl' data-ng-init="mytest='hello'; init();" class="col-sm-9 col-lg-9" >
      </div>
</body>

Comments