简体   繁体   English

将Angular JS数据传递给Button OnClick事件

[英]Pass Angular JS Data to Button OnClick Event

Is it possible to pass angular js data available on a cshtml page to a button click event? 是否可以将cshtml页面上可用的angular js数据传递给按钮单击事件?

the angular js object is referenced as item.property. {{item.property}} 角度js对象被引用为item.property. {{item.property}} item.property. {{item.property}} works well between the html tags. item.property. {{item.property}}在html标记之间运作良好。

The function is: 该函数是:

function ShowFileDialog(ProjectID)
{
    alert(ProjectID);

}

The cshtml that I would like to do is something like this, but clearly is very wrong. cshtml就是这样,但是显然是非常错误的。

<button id="button-view" onclick="ShowFileDialog({{item.projectId}})"></button>

I already have a work around - so others are not needed. 我已经有工作了-因此不需要其他人。

In your controller, 在您的控制器中

$scope.project= {"Id": 1, "name":"project1"};
$scope.ShowFileDialog = function(ProjectID)
    {
        alert(ProjectID);
    }

then in your html, 然后在您的html中,

<button id="button-view" ng-click="ShowFileDialog(project.Id)">show dialog</button>

If you have a list of projects as such, 如果您有这样的项目列表,

$scope.projects= [{"Id": 1, "name":"project1"},{"Id": 2, "name":"project2"}];

then in your html use ng-repeat as, 然后在您的html中使用ng-repeat作为,

<ul>
    <li ng-repeat="proj in projects>
        {{proj.name}} <button ng-click="ShowFileDialog(proj.Id)">show dialog</button>
    </li>
</ul>

see more here 在这里看到更多

From my POV these type of mixes shouldn't be done. 从我的观点来看,这些类型的混合不应该做。 The closest correct approach should be: 最接近的正确方法应该是:

Setting your view with a value in the way angular was meant to be used: 使用您应该使用的角度值来设置视图

<input type="text" ng-model="myVariable" id="myVar" />

and if you want to you get or use this value get it from the view like: 如果要获取或使用此值,请从如下视图中获取:

<script type="text/javascript">  
    var param1 = document.getElementById("myVal").value;
    samplefunction( param1 );   
</script>

Online Demo 在线演示

Note: In this example I used an input but you could use a data- myVar attribute and get the value from there as well. 注意:在此示例中,我使用了input但是您可以使用data- myVar属性并从中获取值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM