2017-04-18 79 views
0

我實際上試圖顯示/隱藏基於用戶類型的div,假設usertype1將能夠看到div,但usertype2將無法查看div。Javascript - 爲什麼我在使用ng時遇到此錯誤 - 如果在AngularJS中?

HTML代碼

<div class="rate animated rubberBand" ng-if="myCheck"> 
      <input type="radio" id="starRating5" name="rate" value="5" disabled> 
      <label for="star5" title="text"></label> 
      <input type="radio" id="starRating4" name="rate" value="4" disabled> 
      <label for="star4" title="text"></label> 
      <input type="radio" id="starRating3" name="rate" value="3" disabled> 
      <label for="star3" title="text"></label> 
      <input type="radio" id="starRating2" name="rate" value="2" disabled> 
      <label for="star2" title="text"></label> 
      <input type="radio" id="starRating1" name="rate" value="1" disabled> 
      <label for="star1" title="text"></label> 
      </div> 

JS代碼

if (userType === 'userType2') 
    { 
     $scope.myCheck = false; 
    } 
    else 
    { 
     $scope.myCheck = true; 
    } 

JS代碼塊這是給我的錯誤

if($scope.Item.starRating === 'NULL'){ 
     $scope.Item.starRating = 0; 
    } 

    else if($scope.Item.starRating === '1'){ 
     document.getElementById("starRating1").checked = true; 
    } 

    else if($scope.Item.starRating === '2'){ 
     document.getElementById("starRating2").checked = true; 
    } 

    else if($scope.Item.starRating === '3'){ 
     document.getElementById("starRating3").checked = true; 
    } 

    else if($scope.Item.starRating === '4'){    
     document.getElementById("starRating4").checked = true; 
    } 
    else{ 
     document.getElementById("starRating5").checked = true; 
    } 

的錯誤

無法設置屬性空的「檢查」

我只是想顯示userType1 div和隱藏userType2。 我沒有使用JQuery。

UPDATE

我與Pengyy的解決方案獲得了唯一的問題 - 這個問題是固定的userType1其中DIV節目,但與userType2我現在面臨的問題,其中股利應不顯示。在userType2中,儘管myCheck是假的,無論是使用ng-show還是使用ng-cloak和ng-show,div都會顯示一會兒然後消失。它根本不應該顯示userType2

+0

如果你不使用jQuery,你爲什麼用它標記你的問題? –

+0

@Sterling弓箭手它已被刪除 – msm0204

+0

如果元素不存在,你如何引用它? – epascarello

回答

5

隨着ng-if,當表達式爲false時,元素將從DOM中刪除。 文檔here

這將導致document.getElementById()沒有得到任何回報。 考慮一下你的情況,你應該試試ng-show

<div class="rate animated rubberBand" ng-cloak ng-show="mycheck"> 
    ... 
</div> 
+0

你能告訴我怎麼做?添加了 – msm0204

+0

@ msm0204示例代碼。 – Pengyy

+0

我可以像使用ng-if一樣使用ng-show腳本文件中的userType檢查嗎? – msm0204

0

var app = angular.module('plunker', []); 
 
app.controller('MainCtrl', function($scope,$log) { 
 
$scope.userType = 'userType1'; 
 
$scope.mycheck = true; 
 
$scope.change=function(){ 
 
if ($scope.userType == 'userType2') 
 
    { 
 
     $scope.mycheck = false; 
 
    } 
 
    else 
 
    { 
 
     $scope.mycheck = true; 
 
    } 
 
    //$log.info($scope.userType); 
 
} 
 

 
    
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="plunker"> 
 
<div ng-controller="MainCtrl" > 
 
<select ng-model="userType" ng-change="change()"> 
 
      <option value="userType1">userType1</option> 
 
      <option value="userType2">userType2</option> 
 
      </select> 
 
<div class="rate animated rubberBand" ng-if="mycheck"> 
 
      <input type="radio" id="starRating5" name="rate" value="5" disabled> 
 
      <label for="star5" title="text"></label> 
 
      <input type="radio" id="starRating4" name="rate" value="4" disabled> 
 
      <label for="star4" title="text"></label> 
 
      <input type="radio" id="starRating3" name="rate" value="3" disabled> 
 
      <label for="star3" title="text"></label> 
 
      <input type="radio" id="starRating2" name="rate" value="2" disabled> 
 
      <label for="star2" title="text"></label> 
 
      <input type="radio" id="starRating1" name="rate" value="1" disabled> 
 
      <label for="star1" title="text"></label> 
 
      </div> 
 
      
 
      </div> 
 
      </div>

+0

我不能使用這個 - $ scope.userType ='userType1';在我的應用程序。因爲我必須把if-else塊用於usertype檢查。因爲我要在if-else語句塊中設置其他條件,爲了簡單起見,我沒有寫在問題部分 – msm0204

+0

我得到的唯一問題是Pengyy的解決方案 - 問題是用userType1修復的,其中div顯示但是現在我遇到了不能顯示div的userType2問題。在userType2中,儘管myCheck是假的,無論是使用ng-show還是使用ng-cloak和ng-show,div都會顯示一會兒然後消失。它根本不應該顯示userType2 – msm0204

0

你需要的方式來跟蹤用戶類型的變化,這只是處理USERTYPE當控制器負載

$scope.userType = 'userType1'; 

if ($scope.userType=== 'userType2') 
    { 
     $scope.myCheck = false; 
    } 
    else 
    { 
     $scope.myCheck = true; 
    } 

所以你需要添加$手錶觀看用戶類型變化

$scope.$watchCollection("userType ", function(newVal, oldVal){ 

     $scope.userType = newVal; 
     if ($scope.userType=== 'userType2') 
     { 
      $scope.myCheck = false; 
     } 
     else 
     { 
      $scope.myCheck = true; 
     } 

     } 
相關問題