如何使用angular JS创建一个新数组?

时间:2014-10-04 13:40:11

标签: javascript arrays angularjs

<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>

1 个答案:

答案 0 :(得分:4)

要创建名称存储在变量中的对象属性,您应该使用括号表示法。在你的情况下:

function controller($scope) {
    $scope.create = function(id) {
        $scope[id] = [];
        // do something with new array
    };
}