AngularJS : ng-include and ng-controller

前端 未结 3 468
抹茶落季
抹茶落季 2020-12-14 16:26

I have an app which I am building with angular, I have about 8-10 views to build out. All the views have a shared footer, based on the view and a set of business rules i nee

3条回答
  •  盖世英雄少女心
    2020-12-14 16:59

    You've misunderstood what the controller as syntax (see documentation) is used for. It is just a way to expose a particular controller on your local scope, so that you can access its properties from a template. When you use someController as vm in both your parent and footer templates, you don't somehow create a connection between the controllers or anything like that. You're just setting a vm property on your footer's scope, so when you use it in your footer template, you're accessing the footer's controller (and you've blocked your way to the parent controller).

    For what you're trying to do, you basically don't need the controller as syntax at all. Just properly put your data on $scope and let the scope hierarchy do the rest for you.

    In your parent controller:

    $scope.features.rock = true;
    $scope.features.roll = false;
    

    In your footer template

    ...

    ...

    You can now also see and change the features from your other controllers (as their scopes are descendants of parent controller's scope).

提交回复
热议问题