2016-11-21 18 views
1

我正在使用ng值爲對象設置無線電輸入值,因此當值與ng模型對象匹配時,它應該導致無線電被檢查。這裏有一個例子和plunker。使用ng值加載時取消選中角度無線電輸入

<script> 
angular.module('test', []) 
.controller('testController', ['$scope', function($scope) { 

    $scope.Obj = { 
    "id": "1", 
    "value": "green" 
    }; 
    $scope.ObjTwo = { 
    "id": "2", 
    "value": "red" 
    }; 
    $scope.ObjThree = { 
    "id": "3", 
    "value": "blue" 
    }; 

    $scope.modelObj = { 
    "id": "3", 
    "value": "blue" 
    }; 

}]); 
</script> 

<form name="myForm" ng-controller="testController"> 
    <label> 
    <input type="radio" ng-model="modelObj" ng-value="Obj"> 
    {{Obj}} 
    </label><br/> 
    <label> 
    <input type="radio" ng-model="modelObj" ng-value="ObjTwo"> 
    {{ObjTwo}} 
    </label><br/> 
    <label> 
    <input type="radio" ng-model="modelObj" ng-value="ObjThree"> 
    {{ObjThree}} 
    </label><br/> 

    color = {{modelObj}}<br/> 

https://plnkr.co/edit/aAISUTEqdGkpVrEJt0uq?p=preview

看來點擊單選按鈕,當作爲modelObj更新到相關目標做工精細。但爲什麼不是在加載時檢查第三個無線電輸入(objThree)?我想,因爲$ scope.Objthree等於$ scope.modeObj在控制器中設置,它將檢查無線電輸入?

回答

1

工作例如:https://plnkr.co/edit/J3QQM0z0nSC9KulHzQGI?p=preview

嘗試$scope.modelObj = $scope.ObjThree;。這會將$scope.modelObj的引用設置爲等於ObjThree對象,而不是僅使用恰好具有相同屬性的新引用創建新對象。通常,JavaScript使用引用相等來比較對象,並僅使用值相等來比較基本數據類型,如數字和字符串。

0

你可以在ngInit設置:

ng-init="modelObj = ObjThree" 

可能是:

<form name="myForm" ng-controller="testController" ng-init="modelObj = ObjThree"> 
相關問題