0

I have this plunker: http://plnkr.co/edit/FnCTfZf8RVBx2WVscyK8?p=info

if I change the line/s(around 23)

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

to

app.controller('MainCtrl', function ($scope, $http) {
            $http.get('data.json')
                .success(function(data, status, headers, config) {
                    $scope.links = data;
                });

I don't see any data.

I guess this happens because the data is set after the ui has already been rendered.
How do I make the data binding work corectlly?

Thanks

3
  • I don't see any problem with your code. plunkr Commented Feb 23, 2014 at 0:31
  • @Sai the data from the json is not shown. Commented Feb 23, 2014 at 1:11
  • ohh you mean the submenu object... I thought about links object which i was seeing properly.. Anyways I have updated in the answer verify. Hope this helps. Commented Feb 23, 2014 at 2:29

1 Answer 1

2

The issue you are facing is different reference of variables. i.e when you say

a = b

Then when you modify "b", "a" is not going to change. Hence, in ng-init you have just initialized with value

submenu = links

When "links" gets updated then "submenu" does not.

So, here you can setup watch on scope variable "links", which when updated you can update "submenu".

Please find the plunkr for the same.

Code:

$scope.$watch('links',function(newValue){
   $scope.submenu=newValue;
});
Sign up to request clarification or add additional context in comments.

Comments

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.