0

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.

6
  • try console.log($scope.mytest); inside your controller Commented Jun 1, 2016 at 5:03
  • what is detailUrl ? where is $scope.init() called? Commented Jun 1, 2016 at 5:03
  • console.log($scope.mytest); it is not ok . kamal pal Commented Jun 1, 2016 at 5:06
  • what you getting in console ? Commented Jun 1, 2016 at 5:07
  • So sorry Guillaume .I now updated my question.Thanks Commented Jun 1, 2016 at 5:08

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

4 Comments

Thank iyerrama25.Your solution is ok for me.
I am now studying angular js.I want to know good reference address or book for angular js,javascript,php.Can you help me.Thank iyerrama25.
@MrMyo Even I am pretty new to the angular stuffs, the way I learned is by referencing the angularjs site itself: (angularjs.org) It has some amazing links to its docs and videos (docs.angularjs.org/tutorial) (youtube.com/user/angularjs, you can read up on it. Other sources: github.com/jmcunningham/AngularJS-Learning & github.com/johnpapa/angular-styleguide
but outside init function $scope.mytest value shown as undefined

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.