AngularJS - 在Json字符串中添加选择模型选项

时间:2016-07-06 10:09:43

标签: angularjs json angularjs-select

HTML

<form ng-submit="exportVideoForm()">
    <input type="text" class="form-control" ng-model="exportvideo.exportName" required>

    <input type="text" class="form-control" ng-model="exportvideo.description" required>

    <select class="form-control" name="selectposition" id="selectposition" 
          ng-options="position.name for position in data.waterMarkImgPosition track by position.id"
          ng-model="data.selectedPosition"
          ng-change="changePosition(data.selectedPosition)"
          required>
    <option ng-show="false" value="">Please Select</option>
    </select>
</form>

控制

$scope.exportVideoForm = function() {

        $scope.data.selectedPosition.id = 'lowerLeft';
        $scope.exportvideo = $scope.exportvideo.push($scope.data.selectedPosition.id); //push watermark position in json string
        $scope.exportVideoJson = angular.toJson($scope.exportvideo); //convert to json

};

输出

console.log($scope.exportVideoJson) = {"exportName":"myname","description":"mydesc"}

当我尝试concat/push position.id时,它会抛出异常 错误:$ scope.exportvideo.push不是函数

如何将position.id值添加到现有的json字符串?

1 个答案:

答案 0 :(得分:0)

Push用于将项目附加到数组。看起来您想要将属性添加到json对象。只是做

$scope.exportvideo.id = $scope.data.selectedPosition.id;