Different ng-include's on the same page: how to send different variables to each?

前端 未结 15 1345
北海茫月
北海茫月 2020-12-04 12:17

I\'ve got a page in my AngularJS app in which I would like to include the same html partial, but with different variables. If I do this in my main html:

15条回答
  •  春和景丽
    2020-12-04 12:54

    You may do it refer to features single values like $scope.a = 'test' and for objects like $scope.a = {a.test}; and use different scope to define visiblity area.

    From ng-if documentation.

    Note that when an element is removed using ngIf its scope is destroyed and a new scope is created when the element is restored.

    Simpe solution is to include ng-if and define you static conetent into ng-init directive.

    In this case it will be show with different values like

    {{val}} shows >>
    First
    Second
    

    This Content is static because val is simpe reference to value.

    If you want to have dynamically changeable pages which depends on some common scope values you need to destroy and recreate scope that has been created when used directive ng-if. Like this:

    and

    $scope.orders_client = null;
       /*recreate content table*/
         $timeout(function () {
            $rootScope.general.showPreloader = false;
            $scope.orders_client = $scope.baseData.clients[newClient];
         }, 500);
    

    In this case scope with previously data will be destroyed and new scope with new data will be created. In need to time to recreate new scope 500ms. Please write if some better catch exist.

提交回复
热议问题