2015-01-21 58 views
1

我有一個文本框。當我在該文本框中鍵入內容並單擊任何地方時,文本框中的內容都希望使用JSON進行推送。 這裏是我的文本框代碼使用angularjs將數據推送到ng-blur上的JSON

<input type="text" name="treatmentCost" class="form-control" ng-model="addTemplate" /> 

這裏是我的JSON

$scope.existingTemp = [{"name": "template1"},{"name": "template2"},{"name": "template3"},{"name": "template4"},{"name": "template5"}]; 

請分享您的想法。提前致謝。

回答

0

可以使用ngBlur指令爲。

查看:

<input type="text" ng-model="addTemplate" ng-blur="addValueToJson(addTemplate)"/> 

控制器:

$scope.addValueToJson = function(value){ 
    var newObj = {name: value}; 
    $scope.existingTemp.push(newObj); 
}; 

這是工作JSFiddle你。

希望它有幫助。

0
<!DOCTYPE html> 
<html ng-app="plunker"> 

<head> 
    <meta charset="utf-8" /> 
    <script data-require="[email protected]" src="https://code.angularjs.org/1.3.10/angular.js" data-semver="1.3.10"></script> 
    <script> 
    var app = angular.module('plunker', []); 

    app.controller('MainCtrl', function($scope) { 
     $scope.data=[]; 
     $scope.fill=function(){ 
     $scope.data.push("name:"+$scope.addTemplate); 
     console.log(JSON.parse(JSON.stringify($scope.data))); 
     } 
    }); 
    </script> 

</head> 

<body ng-controller="MainCtrl"> 
<input type="text" name="treatmentCost" class="form-control" ng-model="addTemplate" ng-blur="fill()"/> 
</body> 
</html> 

Plunker:http://plnkr.co/edit/rR0Sl0ZE7xTrZmIonVAI?p=preview

相關問題