2017-10-10 6 views
0

j'ai une telle directiveAngularJS: Appel directive du contrôleur avec certaines valeurs

app.directive('pagination',function() { 
    //custom directive for build pagination 
    return { 
     restrict: 'E', 
     template:function (elem,attr) { 
      console.log(attr.pageCount); 
      return 'pagination here'; 
     } 
    }; 
}) 

et son renderd dans mon html comme celui-ci

<pagination pageCount="2" currentPage="currentPage"></pagination> 

Mais je veux rendre cela après un appel http de mon contrôleur

$http.post('/search',searchParams).then(function (response) { 
    //render `pagination` from here 
    }) 

Répondre

1

AngularJS n oumalise la balise et le nom d'attribut d'un élément pour déterminer quels éléments correspondent aux directives. Nous référons généralement aux directives par leur nom normalisé camelCase sensible à la casse (par exemple ngModel). Toutefois, comme le code HTML est insensible à la casse, nous référons aux directives dans le DOM par des minuscules, en utilisant généralement des attributs délimités par des tirets sur les éléments DOM (par exemple ng-model).

Essayez avec ng-si ..

<pagination page-count="2" current-page="currentPage" ng-if="showPage"></pagination> 

    $http.post('/search',searchParams).then(function (response) { 
    //render `pagination` from here 
    $scope.showPage = true; 
    }) 

(function(angular) { 
 
    'use strict'; 
 
angular.module('docsTransclusionExample', []) 
 
    .controller('Controller', ['$scope', function($scope) { 
 
    $scope.name = 'Tobias'; 
 
    }]) 
 
    .directive('pagination',function() { 
 
    //custom directive for build pagination 
 
    return { 
 
     restrict: 'E', 
 
     template:function (elem, attr) { 
 
      console.log(attr.pageCount); 
 
      // console.log(attr.pagecount); 
 
      return 'pagination here'; 
 
     } 
 
    }; 
 
}); 
 
})(window.angular); 
 

 
/* 
 
Copyright 2017 Google Inc. All Rights Reserved. 
 
Use of this source code is governed by an MIT-style license that 
 
can be found in the LICENSE file at http://angular.io/license 
 
*/
<!doctype html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Example - example-directive-transclusion-production</title> 
 
    
 

 
    <script src="//code.angularjs.org/snapshot/angular.min.js"></script> 
 
    <script src="script.js"></script> 
 
    
 

 
    
 
</head> 
 
<body ng-app="docsTransclusionExample"> 
 
    <div ng-controller="Controller" ng-init="hid = false">{{hid}} 
 
<pagination ng-if="hid" page-count="2" current-page="currentPage"></pagination> 
 
    <button ng-click="hid=true">Click!</button> 
 
</div> 
 
</body> 
 
</html>

+0

Mais je suis arrivé 'valeur pageCount' comme undeifned dans la directive' attr.pageCount' – Jabaa

+0

Salut voir la réponse mise à jour. .. –

+0

use directive comme ci-dessus. –