javascript - Bind part of string to Angular scope variable -
i'm pulling json data server. inside json object i'm returning, have strings want able use placeholders dynamically insert values entered user in ui.
my understanding $compile for, can't seem work. i'm not sure if possible or if i'm approaching wrong way.
edit: not sure if i'm explaining well. got little further , updated plunk , code below
a simplified example (view plunk):
view:
<body ng-controller="mainctrl"> <input ng-model="name" type="text" /> <p ng-bind-html="myval">{{myval}}</p> <p>{{name}}</p> </body>
angular app:
var app = angular.module('plunker', ['ngsanitize']); app.controller('mainctrl', function($scope, $compile, datasvc) { init(); function init() { $scope.data = datasvc.getdata(); $scope.name = ''; } $scope.$watch('name', function(newval, oldval) { var c = $compile('<span>' + $scope.data.vals[0].text + '</span>')($scope); console.log(c); console.log(c[0]); $scope.myval = c[0]; }); }); app.service('datasvc', function () { this.getdata = function () { return { vals: [ { text: "hello {{name}}" } ] } }; });
this works $compile , console logs changes way want them happen, can't output on display.
i'd suggest use $interpolate
service while assigning variable other, take care of evaluation of curly braces.
code
var app = angular.module('plunker', []); app.controller('mainctrl', function($scope, $interpolate) { $scope.test = "hello {{name}}!"; $scope.name = 'world'; init(); function init() { $scope.concat = $interpolate($scope.test)($scope); } });
Comments
Post a Comment