2
votes

Ajouter la valeur ng-model dans la condition fausse de ng-attr-title

Comment ajouter mainCtrl.header.Version dans la chaîne de texte si la condition dans ng-attr-title est fausse?

HTML

<label class="form-control" ng-attr-title="{{mainCtrl.header.Version == 0 ? 'This form has not yet been submitted for approval' : 'This form has been submitted for approval {{mainCtrl.header.Version}} times'}}">{{mainCtrl.header.Version}}</label>


4 commentaires


@PierreEmmanuelLallemant Merci pour la solution suggérée. Est-ce possible en utilisant simplement ng-attr-title?


ng-attr-title = "{{mainCtrl.header.Version == 0? 'Ce formulaire n'a pas encore été soumis pour approbation': 'Ce formulaire a été soumis pour approbation' + mainCtrl.header.Version + ' fois '}} " devrait fonctionner


@PierreEmmanuelLallemant J'ai déjà essayé cela mais cela n'a pas fonctionné. Je l'ai réessayé tout à l'heure et cela a fonctionné! J'ai dû manquer quelque chose. Pouvez-vous soumettre ceci comme réponse?


3 Réponses :


0
votes

Vous pouvez y parvenir du côté du contrôleur JS. Prenez une variable title vérifiez si version est 0 puis attribuez le titre que vous voulez Et si la version est supérieure à 0, concattez la version avec le message de titre. Veuillez prendre en compte l'extrait de code suivant.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl as mainCtrl">
<label class="form-control" ng-attr-title="{{mainCtrl.title}}">
{{mainCtrl.header.Version}} 
</label>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    var mainCtrl = this;
    mainCtrl.header = {"Version":0};
    if( mainCtrl.header.Version == 0 )
    {
    	mainCtrl.title = "This form has not yet been submitted for approval";
    }
    else
    {
    	mainCtrl.title = "This form has not yet been submitted for " +mainCtrl.header.Version+"  approval";
    }
});
</script>
</body>
</html>


0 commentaires

0
votes

vous pouvez le faire en appelant une fonction comme celle-ci:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

<div ng-app="app">
  <div ng-controller="htmlTitle">
    
    <label class="form-control"  title="{{get_title()}}" >{{mainCtrl.header.Version}}</label>
  </div>
</div>
var app = angular.module('app', []);
app.controller('htmlTitle', ['$scope','$http', function($scope, $http){
  
  $scope.mainCtrl = {
        header: {
        	Version: 0
        }
    };
  
  $scope.get_title = function(){
    if($scope.mainCtrl.header.Version == 0){
      return 'This form has not yet been submitted for approval';
    }
    else{
      return 'This form has been submitted for approval ' +  $scope.mainCtrl.header.Version + ' times';
    }
  }
}]);

et voici un codepen . p >


0 commentaires

1
votes

La syntaxe est:

ng-attr-title="{{mainCtrl.header.Version == 0 ? 'This form has not yet been submitted for approval' : 'This form has been submitted for approval ' + mainCtrl.header.Version + ' times'}}"

ng-attr-title affiche la chaîne qui lui est donnée, vous devez donc utiliser {{expr}}. Pour tester votre expression, je vous conseille d'écrire

  pour voir quel est le résultat. 


0 commentaires