简体   繁体   English

如何将args从html传递到js?

[英]How to pass args from html to js?

I'm trying to pass the value of an input this way: 我试图通过这种方式传递输入的值:

html html

<tr ng-controller="fCtrl as fiche">
    <td>
        <span>
            <input type="checkbox" name="q" ng-model="qo" >
            <input type="checkbox" name="q" ng-model="qa" >
        </span>
    </td>
    <td>
        <button type="submit" class="btn btn-primary" ng-click="fiche.submitEdit(q)">Modifier</button>
    </td>
</tr>

js js

(function(){
    var app = angular.module('m', [ ]);

    app.controller('fCtrl', function(){
        this.submitEdit = function(q){
             alert(q);
        }
    });

})();

But, doing that I can't catch anything. 但是,这样做我什么都抓不到。 I've also tried using fiche.submitEdit({{q}}) , but it wouldn't work. 我也尝试过使用fiche.submitEdit({{q}}) ,但这是行不通的。 Any brilliant idea, please? 有什么好主意吗?

Thanks a lot! 非常感谢!

When you are using ng-model no need to pass the data, try it 当您使用ng-model无需传递数据时,请尝试

<table ng-app="m">
<tr ng-controller="fCtrl">
<td>
    <span>
        <input type="checkbox" name="q" ng-model="qo" >
        <input type="checkbox" name="q" ng-model="qa" >
    </span>
</td>
<td>
    <button type="submit" class="btn btn-primary" ng-click="submitEdit()">Modifier</button>
</td>
</tr>
</table>
(function(){
var app = angular.module('m', [ ]);

app.controller('fCtrl', function($scope){
    $scope.qo = "";
    $scope.submitEdit = function(){
         alert($scope.qo);
    }
});

})();

I have never working with controller aliases. 我从未使用过控制器别名。 This should work? 这应该工作吗?

(function(){
var app = angular.module('m', [ ]);

app.controller('fCtrl', ['$scope',function($scope){
    $scope.submitEdit = function(){
         alert($scope.qo);
    }
}]);

})();

And in html just call ng-click="submitEdit()" 在html中,只需调用ng-click =“ submitEdit()”

To start with , always use a dot in ng-model . 首先,请始终在ng-model使用一个点。 It almost appears that is what you were wanting to do and that you would have one object q when you submit. 几乎看起来这就是您想要做的,提交时将只有一个对象q

The powerful part of ng-model is it will create the scope objects if they don't already exist. ng-model的强大功能是,如果范围对象尚不存在,它将创建范围对象。

In your case if you changed the ng-model 's to qa and qo your method would work and you would have one object q in scope with properties a and o . 在您的情况下,如果将ng-model的值更改为qaqo您的方法将起作用,并且您将在作用域中拥有一个对象q和属性ao

However you have also set up a controller alias so now you need to prefix all your html scope variables with that controller alias 但是,您还已经设置了控制器别名,因此现在您需要在所有html范围变量之前添加该控制器别名

<input type="checkbox" name="q" ng-model="fiche.q.o" >
<input type="checkbox" name="q" ng-model="fiche.q.a" >

<button ng-click="fiche.submitEdit(fiche.q)">


 $scope.submitEdit = function(q){
     alert($scope.q.o);
}

There really is no need to pass the object as argument since it already exists in scope as $scope.q 确实不需要将对象作为参数传递,因为它已经在$scope.q作用域中存在。

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

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