JoethaCoder JoethaCoder - 1 year ago 109
AngularJS Question

NG-INIT not working properly

I have tried several ways, but for some reason, I am not able to get the init method to work on a tab application I am building.

I am pulling in JSON using http (via WordPress REST API) and had to use the 'as' syntax to get it working properly. Everything works once I click the initial tab, but I need the first tab to load on page load.

Here is the App:


var homeApp = angular.module('homeCharacters', ['ngSanitize']);
homeApp.controller('characters', function($scope, $http) {
$http.get("").then(function(response) {
$scope.myData =;
homeApp.filter('toTrusted', ['$sce',
function($sce) {
return function(text) {
return $sce.trustAsHtml(text);


<section class="characters" ng-app="homeCharacters" ng-controller="characters as myData">
<div class="char_copy" ng-repeat="item in myData" ng-bind-html="item.content | toTrusted" ng-show=" === item.menu_order">
{{ item.content }}
<div class="char_tabs">
<ul ng-init=" = 0">
<li ng-repeat="item in myData">
<a href ng-click=" = item.menu_order">
<img src="{{ item.featured_image.source }}" />
<h3>{{ item.title }}</h3>
<!--end characters-->

I am assuming there is something simple I am missing. The menu_order content for the tab I want to init is 0. In ng-inspector under MyData it shows tab

I would appreaciate any help anyone could offer!

Answer Source

Currently what happening is when UI gets rendered on page, ng-init directive gets processed first and it evaluates = 0 and set myData's tab property value to 0. Thereafter when asynchronous API completes retrieved data gets assigned to myData object again which wipes out old myData value.

You shouldn't be using ng-init for this case. You should set data retrieved from ajax to some another property of myData from controller itself.


homeApp.controller('characters', function($scope, $http) {
  $scope.myData = { tab: 0 }; //set default tab
  $http.get("").then(function(response) {
    $ =;


<li ng-repeat="item in">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download