angular.js第二个项目 绑定

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
名字: <input ng-model="name">
<h1>{{name}}</h1>
    
<table >
    <tr>
        <th  ng-repeat="y in attrlist">
            {{y.name}}
        </th>    
    </tr>
</table>    
<table ng-repeat="y in attrlist">
    <tr   ng-repeat="x in y.attr_values">
        <td>
            {{y.name}}{{x.v_name}}
        </td>            
        <td>
            价格:<input name="{{x.attrid}}-price">
        </td>    
    </tr>
</table>    
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.name = "John Doe";
    $scope.attrlist=[{attrid:1,name:'颜色',attr_values:[{v_id:1,v_name:'白色'},{v_id:2,v_name:'黑色'}]},{attrid:2,name:'内存容量',attr_values:[{v_id:3,v_name:'16G'},{v_id:4,v_name:'32G'}]},{attrid:3,name:'网络类型',attr_values:[{v_id:5,v_name:'移动'},{v_id:6,v_name:'联通'}]}];
});
</script>

<p>使用 ng-model 指令来绑定输入域的值到控制器的属性。</p>

</body>
</html>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
名字: <input ng-model="name">
<div ng-repeat="x in attrlist">
<input type="checkbox" name="{{x.name}}" ng-true-value="x.attrid" ng-false-value="0" ng-model="x.attrid" ng-checked="isSelected(x.attrid)" ng-click="updateSelection($event,x.attrid)"/>{{x.name}}

</div>
<pre>{{selected|json}}</pre>
<pre>{{selectedTags|json}}</pre>
<pre>{{skus|json}}</pre>
<table>
<tr >
<th ng-repeat="tag in selectedTags">{{tag}}</th>
</tr>
<tr >
<td><input type="text" /></td>
</tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = "John Doe";
$scope.alert=function($event){
console.log($event.target);
};
$scope.selectedTags = [];
$scope.selected = [];
$scope.skus = [];
$scope.attrlist=[{attrid:1,name:'颜色',attr_values:[{v_id:1,v_name:'白色'},{v_id:2,v_name:'黑色'}]},{attrid:2,name:'内存容量',attr_values:[{v_id:3,v_name:'16G'},{v_id:4,v_name:'32G'}]},{attrid:3,name:'网络类型',attr_values:[{v_id:5,v_name:'移动'},{v_id:6,v_name:'联通'}]}];

var updateSelected = function(action,id,name){
if(action == 'add' && $scope.selected.indexOf(id) == -1){
$scope.selected.push(id);
$scope.selectedTags.push(name);
}
if(action == 'remove' && $scope.selected.indexOf(id)!=-1){
var idx = $scope.selected.indexOf(id);
$scope.selected.splice(idx,1);
$scope.selectedTags.splice(idx,1);
}

$scope.skus=[];
updateSkus(action,id,name);
}

var updateSkus=function(action,id,name){


}

$scope.updateSelection = function($event, checked){
var checkbox = $event.target;
var action = (checkbox.checked?'add':'remove');

updateSelected(action,checked,checkbox.name);
}

$scope.isSelected = function(id){
return $scope.selected.indexOf(id)>=0;
}
});
</script>

<p>使用 ng-model 指令来绑定输入域的值到控制器的属性。</p>

</body>
</html>

原文地址:https://www.cnblogs.com/westfruit/p/5210807.html