当前位置: 动力学知识库 > 问答 > 编程问答 >

angularjs - button assigned to scope variable in controller and binded to html with expression now i am clicking button not called function , how can do it?

问题描述:

scope.html = " {{data}} ".Here {{data}} expression not working and when clicked button getUserDetails function not calling. how can i do this please help me.

I tried code

 html

----

<div ng-controller="MyCtrl">

<form>

<input type="text" ng-model="user.name" />

<input type="button" ng-click="sayName(user.name);" value="sayName"/>

</form>

<span ng-bind-html-unsafe="html"></span>

</div>

script

-----

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {

$scope.sayName = function(data){

$scope.html = "<div><button ng-click='getUserDetails('"+data+")'> {{data}}</button></div>";

}

$scope.getUserDetails = function(data){

alert(data)

}

}

I added code in jsfiddle can see once please

网友答案:

Is this what you are trying to do? See updated fiddle http://jsfiddle.net/wboxqqu0/3/

html
----
<div ng-controller="MyCtrl">
  <form>
     <input type="text" ng-model="user.name" />
     <input type="button" ng-click="sayName(user.name);" value="sayName"/>
  </form>
   <div ng-repeat="name in data track by $index"><button ng-show="name.length > 0" ng-click='getUserDetails(name)'> {{name}}</button>
</div>

script
-----
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
 $scope.data=[];
 $scope.sayName = function(data){
 $scope.data.push(data);
 }
 $scope.getUserDetails = function(data){
 alert(data)
 }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: