parsing JSON with / character in angularjs -


i have problem in displaying "/home/local" there in following json

"parameter":{"name":"localdir","default_value":"/home/local","type":"string"} 

i need parse , display in text box using angularjs

'<input type = "text" value="' +   jsondata["default_value"] +   '" name="htmlcomponent" ng-model="htmlcomponent" ng-init="htmlcomponent=' +   jsondata["default_value"] +   '" class="htmlcomponent" />' 

when tried getting following error:

error: [$parse:syntax] syntax error: token 'home' unexpected token @ column 16 of expression [htmlcomponent=/home/local] starting @ [home/local].

the script below demonstrates, code does:

var jsondata = {    "name": "localdir",     "default_value": "/home/local",     "type": "string"  };    var input = '<input type = "text" value="' +    jsondata["default_value"] +    '" name="htmlcomponent" ng-model="htmlcomponent" ng-init="htmlcomponent=' +    jsondata["default_value"] +    '" class="htmlcomponent" />';    document.body.innertext = input;

the problematic part ng-init="htmlcomponent=/home/local" tells angular execute expression htmlcomponent=/home/local upon initialization. error message shown, because not valid expression.
suppose want write string htmlcomponent right expression htmlcomponent='/home/local'.

var jsondata = {    "name": "localdir",     "default_value": "/home/local",     "type": "string"  };    var input = '<input type = "text" value="' +    jsondata["default_value"] +    '" name="htmlcomponent" ng-model="htmlcomponent" ng-init="htmlcomponent=\'' +    jsondata["default_value"] +    '\'" class="htmlcomponent" />';    document.body.innertext = input;

however, solution not perfect, e.g. jsondata["default_value"] still not allowed contain quotation marks.

you should consider use pure angular:

var myapp = angular.module('myapp', []);    myapp.constant('jsondata', {    "name": "localdir",    "default_value": "/home/local",    "type": "string"  });    myapp.controller('testcontroller', function($scope, jsondata) {    $scope.htmlcomponent = jsondata['default_value'];  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>    <div ng-app="myapp">    <div ng-controller="testcontroller">      <input type="text" ng-model="htmlcomponent" />    </div>  </div>


Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -