If you want to find out whether HTML element exists in the page or not, then you have to use angular.element property. For in-existed elements, the result of  angular.element will be []. For in-existed functions and variables, we can use null verification.
<button ng-click="isElementExists('divID')">
  Is divID exists ?
</button>
<button ng-click="isElementExists('divID123')">
  Is divID123 exists ?
</button>
<button ng-click="isSampleFunctionExists()">
  Is sampleFunction exists ?
</button>
<button ng-click="isFunction123Exists()()">
  Is function123 exists ?
</button>
<div id="divID">
  This is divID element
</div>
<script>
function myCtrl($scope) {
    $scope.isElementExists = function(id) {
     var myEl = angular.element( document.querySelector('#'+ id ) );
     if(myEl == '[]') {
        alert(false);
     } else {
        alert(true);
     }
    };

    $scope.isSampleFunctionExists= function() {
      alert($scope.sampleFunction != null);
    };

    $scope.isFunction123Exists= function() {
      alert($scope.function123 != null);
    };

    $scope.sampleFunction = function(id) {
      alert(id);
    };
}
</script>

0 comments:

Blogroll

Follow this blog by Email

Popular Posts