ng-if阻止指令方法

时间:2016-11-30 12:21:00

标签: javascript angularjs

我有一个针对Opentoke Library的指令,但是当我使用ng-if时它没有工作,使用ng-if的原因是IOS设备不支持webrtc所以它&#39 ;在DOM加载后显示警告。

<div class="opentok" ng-if="!isMobileView">
  <open-tok-archive></open-tok-archive>
</div>

<a href="javascript:void();" ng-click="myMethod()">click me</a>

<div class="opentok" ng-if="!isMobileView">
  <open-toke-screen-share></open-toke-screen-share>
</div>

没有ng-if每件事情都运转良好。

Direcitve

'use strict';
var session, apiKey, publisher, openTokToken, archiveID;

angular.module('app')
  .directive('openTokArchiv', [function () {
    return {
      restrict: 'EA',
      templateUrl: 'views/pages/openTokArchive.html',
      controller: function (OpenTokService, OTSession, $window, $rootScope, $scope, $routeParams, $cookieStore) {


      $scope.myMethod = function (){
               console.log("---------not-- working ---------")
           }
    };
  }]);

1 个答案:

答案 0 :(得分:0)

使用ng-show / ng-hide代替ng-if。

<强> EDITS

  <div class="opentok" ng-if="!isMobileView">
    <open-tok-archive></open-tok-archive>
  </div>

看看这个plunker Demo link

  

在没有ng-if #working

的情况下尝试此操作
<!DOCTYPE html>
<html>

  <head>
    <script data-require="angularjs@1.5.8" data-semver="1.5.8" src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

    <body ng-app="app" ng-controller="myCtrl" ng-init="variables">
      <div class="opentok">
        <open-tok-archive></open-tok-archive>
      </div>

      <button type="button" ng-click="myMethod()">Toggle</button>

    </body>

</html>
  

使用ng - 如果它不起作用

                 

<body ng-app="app" ng-controller="myCtrl" ng-init="variables">
  <div class="opentok" ng-if="!isMobileView">
    <open-tok-archive></open-tok-archive>
  </div>

  <button type="button" ng-click="myMethod()">Toggle</button>

</body>