“Comment appeler une fonction de contrôleur après que mon objet ait chargé” Réponses codées

Comment appeler une fonction de contrôleur après que mon objet ait chargé

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

<div ng-app="App" ng-controller="MainCtrl" ng-init="increaseCount()">

  <h1>{{ car }} count: {{ carCount }}</h1>

  <iframe iframe-onload="onIframeLoad()" ng-src="{{ iframeSource }}"></iframe>
  
  <button ng-click="loadIframe()">Load Iframe</button>
  
</div>
SAMER SAEID

Comment appeler une fonction de contrôleur après que mon objet ait chargé

var app = angular.module('App', []);

// Allow iframe loading from various sources
app.config(["$sceDelegateProvider", function($sceDelegateProvider) {
    $sceDelegateProvider.resourceUrlWhitelist([
        // Allow same origin resource loads
        "self",
        // Allow YouTube iframes
        "https://www.youtube.com/embed/**"
    ]);
}]);

// Allow a directive "iframe-onload" in HTML
app.directive('iframeOnload', [function(){
return {
    scope: {
        callBack: '&iframeOnload'
    },
    link: function(scope, element, attrs){
        element.on('load', function(){
            return scope.callBack();
        })
    }
}}]);

// Main controller
app.controller('MainCtrl', function($scope, $sce) {
  $scope.car = 'Mercedes';
  $scope.carCount = 0;
  $scope.iframeSource = "";
  
  // INIT
  $scope.increaseCount = function () {
 
    // First wait on iframe load
    $scope.onIframeLoad = function () {
      console.log('Iframe fully loaded');
      
      increaseCount(); // If iframe loaded then execute main INIT logic
    };
    
    
    // INIT body - main INIT logic
    function increaseCount () {
      $scope.$apply('carCount = 10'); // change $scope.carCount to 10
    }
    
  };
  
  // Load iframe when clicked on the button
  $scope.loadIframe = function () {
    console.log("Clicked on the button.");
    $scope.iframeSource = "https://www.youtube.com/embed/Ra__OWuOU1M";
  }
  
});
SAMER SAEID

Réponses similaires à “Comment appeler une fonction de contrôleur après que mon objet ait chargé”

Questions similaires à “Comment appeler une fonction de contrôleur après que mon objet ait chargé”

Plus de réponses similaires à “Comment appeler une fonction de contrôleur après que mon objet ait chargé” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code