2017-04-12 79 views
0

我是Angularjs的新手,並通過執行某些任務來練習它。在這裏,我正在控制器中創建一個對象,並在我的網頁中創建對象值。但是在這裏,ng-show不會評估下面的表達式。但是,如果我在控制器中創建一個變量爲$ scope.ngshow = false;它會工作。請幫助我爲什麼下面的代碼不起作用。ng-show不評估表達式

<!DOCTYPE html> 
<html> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<body> 

<div ng-app="myApp" ng-controller="myCtrl"> 
<ul> 

<li ng-show = "a.name"> Name {{a.name}} </li> 
<li ng-show = "a.id"> Id {{a.id}}</li> 
<li ng-show = "a.address"> Address {{a.address}}</li> 
</ul> 
</div> 

<script> 
var app = angular.module('myApp', []); 
app.controller('myCtrl', function($scope) { 
    $scope.a = { 
    'name' : 'false', 
    'id' : 'true', 
    'address' : 'false' 
    }; 
}); 
</script> 

<p>ng-show didnt accept expressions.</p> 

</body> 
</html> 

感謝您的寶貴時間。

+0

不要使用true或false作爲字符串,刪除引號。 – Phantom

回答

1

,則不應使用truefalse作爲字符串

var app = angular.module('myApp', []); 
 
app.controller('myCtrl', function($scope) { 
 
    $scope.a = { 
 
    'name' : false, 
 
    'id' : true, 
 
    'address' : false 
 
    }; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 

 
<div ng-app="myApp" ng-controller="myCtrl"> 
 
<ul> 
 

 
<li ng-show = "a.name"> Name {{a.name}} </li> 
 
<li ng-show = "a.id"> Id {{a.id}}</li> 
 
<li ng-show = "a.address"> Address {{a.address}}</li> 
 
</ul> 
 
</div> 
 

 
<script> 
 

 
</script> 
 

 
<p>ng-show didnt accept expressions.</p>

+0

非常感謝:-) – User12345

0

距離truefalse。如果你把報價上truefalse刪除單引號,他們是strings.In該案例ng-show正在檢查變量是否已定義。在您的情況下,由於它具有字符串真/假值,因此ng-show正在考慮表達式a因此定義如此。

<!DOCTYPE html> 
 
<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 

 
<body> 
 

 
    <div ng-app="myApp" ng-controller="myCtrl"> 
 
    <ul> 
 

 
     <li ng-show="a.name"> Name {{a.name}} </li> 
 
     <li ng-show="a.id"> Id {{a.id}}</li> 
 
     <li ng-show="a.address"> Address {{a.address}}</li> 
 
    </ul> 
 
    </div> 
 

 
    <script> 
 
    var app = angular.module('myApp', []); 
 
    app.controller('myCtrl', function($scope) { 
 
     $scope.a = { 
 
     'name': false, 
 
     'id': true, 
 
     'address': false 
 
     }; 
 
    }); 
 
    </script> 
 

 
    <p>ng-show didnt accept expressions.</p> 
 

 
</body> 
 

 
</html>

0

好,apperenly這是一個版本衝突。角度ng的這個版本1.4.8顯示支持值爲boolean而不是字符串布爾值。所以,你需要刪除圍繞價值

$scope.a = { 
     'name' : false, 
     'id' : true, 
     'address' : false 
     }; 

var app = angular.module('myApp', []); 
 
app.controller('myCtrl', function($scope) { 
 
    $scope.a = { 
 
    'name' : false, 
 
    'id' : true, 
 
    'address' : false 
 
    }; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 

 
<div ng-app="myApp" ng-controller="myCtrl"> 
 
<ul> 
 

 
<li ng-show = "a.name"> Name {{a.name}} </li> 
 
<li ng-show = "a.id"> Id {{a.id}}</li> 
 
<li ng-show = "a.address"> Address {{a.address}}</li> 
 
</ul> 
 
</div>

但報價,如果您使用的是較低的版本一樣1.2.23然後角度識別字符串值作爲布爾如果值是true或false

var app = angular.module('myApp', []); 
 
app.controller('myCtrl', function($scope) { 
 
    $scope.a = { 
 
    'name' : 'false', 
 
    'id' : 'true', 
 
    'address' : 'false' 
 
    }; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="myCtrl"> 
 
<ul> 
 

 
<li ng-show = "a.name"> Name {{a.name}} </li> 
 
<li ng-show = "a.id"> Id {{a.id}}</li> 
 
<li ng-show = "a.address"> Address {{a.address}}</li> 
 
</ul> 
 
</div>