<html ng-app="">
<body ng-controller="controller">
<button class="btn" ng-click="create('new') >Click</button>
</body>
</html>
<script>
function controller($scope) {
$scope.create = function(id) {
// What will be the code to create a new array
// having name contained in variable?
};
}
<script>
答案 0 :(得分:4)
要创建名称存储在变量中的对象属性,您应该使用括号表示法。在你的情况下:
function controller($scope) {
$scope.create = function(id) {
$scope[id] = [];
// do something with new array
};
}