Reputation: 2439
I have button that I want to disable when local storage is empty
<button ng-show="LS_wimmtkey!==null">{{LS_wimmtkey}}</button>
Button value shows null, but it is shown, why could that be? I tried writing ng-show="1===2"
when it worked correctly, so the problem is with LS_wimmtkey
this is how I use it:
in main.controller.js
:
$rootScope.LS_wimmtkey = localStorage.getItem('LS_wimmtkey');
$rootScope.$watch("LS_wimmtkey", function() {
localStorage.setItem('LS_wimmtkey', $rootScope.LS_wimmtkey);
My main.view is inserted into the review.view (because form and reviews are on the same page, main is for the review listing and reviews are submitted form)
I add values to local storage after submit in review.controller.js
function submit() {
if($rootScope.name!=null) {
var JSONObject = {
"name":$rootScope.name,
"surname":$rootScope.surname,
"email":$rootScope.email,
"review":$rootScope.review
}
var temp={
"name":$rootScope.name,
"surname":$rootScope.surname,
"email":$rootScope.email
}
$scope.localArray.push(temp);
$rootScope.LS_wimmtkey = $scope.localArray;
// $rootScope.localStorageService.set("LS_wimmtkey", $scope.localArray);debugger;
// $rootScope.LS_wimmtkey= localStorageService.get("LS_wimmtkey"); debugger;
var Results = UniversalService.PostReview(JSON.stringify(JSONObject));
}
}
I print console.log($rootScope.LS_wimmtkey);
and it is null, so why button is not hidden?
Upvotes: 0
Views: 88
Reputation: 467
"LS_wimmtkey" belong to $rootScope, please assign this variable to controller scope. because $rootScope does't bing to template.
Fix is:
$scope.LS_wimmtkey = localStorage.getItem('LS_wimmtkey');
$rootScope.$watch("LS_wimmtkey", function() {
localStorage.setItem('LS_wimmtkey', $scope.LS_wimmtkey);
Upvotes: 0
Reputation: 857
yes we cant be check null by LS_wimmtkey!==null
probably its string it always return true
I'd suggest you:
<button ng-show="LS_wimmtkey">{{LS_wimmtkey}}</button>
try this it will work it will work with undefine
null
etc...
Upvotes: 2
Reputation: 1379
Try with this null, I think your null as a string, that's technically incorrect but might help you.
<button ng-show="LS_wimmtkey!=='null'">{{LS_wimmtkey}}</button>
Upvotes: 1