0

I'm sad... I cook porridge of the ax.. Please, if you can - help me deal with my problem. I have this structure in my html code(ng-controller is on wrap tag):

<a ng-repeat="subitem in cur_submenu" ng-href="#/{{subitem.href}}/">{{subitem.name}}</a>

In JS I have: 1) RouteProvider

$routeProvider.
      when('/:lvl1', {
          template:'<div ng-include="htmlUrl">Loading...</div>',
          controller: 'MainCtrl'
      })

2) Controller

function MainCtrl($scope, $http, $routeParams){
  var lvl = window.location.hash.split('/');
  if ($scope.submenu) {
    //if data was fetch earlier, then set currentMenu
    $scope.cur_submenu = $scope.submenu[lvl[1]];
  } else {
    MainCtrl.prototype.fetchData();
  }

  MainCtrl.prototype = {
    fetchData: function(){
      /*
      * Get data about navigation
      */
      $http({method: 'GET', url: 'data/main.json'}).
      success(function(data){
        $scope.menu = data.menu;
        $scope.submenu = data.submenu;
        $scope.cur_submenu = data.submenu[lvl[1]] //current submenu for my location
      });
   }
}

But it is not updating on my page, when I changed my location on a website(hash-nav)... Please help my. Full version of my site: http://amiu.ru

1 Answer 1

1

You don't need to add a function to your Controller with prototyping. Functions called in your view are to be declared on the $scope like so:

function MainCtrl($scope, $http, $routeParams){
  var lvl = window.location.hash.split('/');
  if ($scope.submenu) {
    //if data was fetch earlier, then set currentMenu
    $scope.cur_submenu = $scope.submenu[lvl[1]];
  } else {
    $scope.fetchData();
  }

  $scope.fetchData = function(){
      /*
      * Get data about navigation
      */
      $http({method: 'GET', url: 'data/main.json'}).
          success(function(data){
            $scope.menu = data.menu;
            $scope.submenu = data.submenu;
            $scope.cur_submenu = data.submenu[lvl[1]] //current submenu for my location
          });
   };
}

In all other cases, if you're executing changes on a $scope outside of a $scope function, you'll need to manually call $scope.$apply() to queue a digest and update your view.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.