mccainz
mccainz

Reputation: 3497

How to use Angular data within a quoted string

Briefly: I am trying to pass a string to an angular function on ng-click. The string needs to have embedded data within it from the angular world (the string is composite of a a static fragment and an Angular 'variable')...

<button ng-click="hello('Mr/Ms {{name}}')">Test</button>

Problem: I can't seem to make angular switch back and parse the brackets as anything other than a string. So, how do I fix/reaarange/modify the string so that the embedded Angular content is passed within the string on ng-click.

Desired Outcome On success I should see a javascript alert stating "hello Mr/Ms Samuel"

Plunkr ... http://plnkr.co/edit/vKMCpAjfXbYZtNgxqubv

HTML

<div id="parent" ng-controller="test">
  <p>{{name}}</p>
  <button ng-click="hello('Mr/Ms {{name}}')">Test</button>
</div>

Javascript

$(document).ready(function(){

    var app = angular.module("app",[]);

    app.controller("test",function($scope){
      $scope.name="Samuel";
      $scope.hello=function(val){
        alert("hello " + val);
      };
    });

    angular.bootstrap(document,["app"]);

});

Upvotes: 0

Views: 138

Answers (1)

doodeec
doodeec

Reputation: 2927

just use classic javascript variable, not an angular expression

ng-click="hello('Mr/Ms '+name)"

Upvotes: 2

Related Questions