如何在指令之前执行ng-if,或者使用其他东西而不是ng-if

How to execute ng-if before a directive or use something else than ng-if?

本文关键字:ng-if 其他 指令 执行 或者      更新时间:2023-09-26

我这里的问题是指令在ng-if之前执行。
我怎样才能在指令之前执行ng-if ?

我做错了什么吗?我应该使用其他东西而不是ng-if来显示/隐藏div吗?

http://plnkr.co/edit/Q2wdSnTJHVQbD3fK3O8N?p =预览

script.js

lolModule = angular.module('lolModule',[]);
lolModule.directive('initTest', function(){
    return function($scope, $element){
        console.log("test",$element.html());
    };
});
lolModule.controller("ctrl",function($scope){
  $scope.testExpr=function(e){
    console.log("bool=",e);
    return e;
  }
});

index . html

<!DOCTYPE html>
<html data-ng-app="lolModule">
  <head>
    <script src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
    <script src="script.js"></script>
  </head>
  <body data-ng-controller="ctrl">
    <div init-Test>
      <div ng-if="testExpr('true')">if is true</div>
      <div ng-if="testExpr('false')">if is true</div>
    </div>
  </body>
</html>

你的函数需要实际返回一些东西,否则它将总是返回undefined(这是假的),这就是为什么没有你的ng-if被触发。

例如:

lolModule.controller("ctrl",function($scope){
  $scope.testExpr=function(e){
    console.log("bool=",e);
    return e == 'true';
  }
});

请看这里:http://plnkr.co/edit/gPizAfnD0BrmHFAfcNyu?p=preview