简体   繁体   English

将值传递给ng-init中的函数

[英]Pass value to a function in ng-init

I want to repeat data using div and in this div , I have one more repeatation using id value from first repeatation. 我想使用div重复数据,在这个div中,我还有一个重复使用第一次重复的id值。 So I want to pass this value using function in ng-init but got me error. 所以我想在ng-init中使用函数传递此值,但出现错误。

        <div ng-controller="ProductsCtrl" ng-init="getData()">         
        <div class="col-sm-12" ng-repeat="data in form">
            <div class="card">
                <div class="card-header">
                    <h4>{{data.title}}</h4>
                </div>          
                <div class="card-block">
                    <div class="row" ng-init="getImages({{data.id}})">
                        <div class="form-group col-sm-3" ng-repeat="image in images">
                            <figure>
                              <img ng-src="{{imgURL}}/{{image.file_name}}" width="220" height="176">&nbsp;
                              <figcaption>Fig.1 - A view of the pulpit rock in Norway.</figcaption><br />
                              <label>Description : {{image.remarks}}</label><br />
                              <a href="">Read More . .</a>
                            </figure>
                        </div>
                    </div>
                </div>
                <div class="card-footer">
                    <h4>{{data.description}}</h4>
                </div>             
            </div>           
        </div>
    </div> 

So, how can I pass {{data.id}} in ng-init="getImages({{data.id}})"? 因此,如何在ng-init =“ getImages({{data.id}}))中传递{{data.id}}? If I put value ng-init="getImages(15)" then, item will appear. 如果我将值ng-init =“ getImages(15)”放进去,则将显示项目。

file.js file.js

    $scope.getImages = function (id2) {

    $http.get(commonData.apiURL + 'product/detailsImage.php?id='+id2)
        .success(function (data) {
            $scope.images = data;
            //console.log(JSON.stringify(data));
        })
        .error(function (data, status, headers, config) {
            $scope.errorMessage = "Couldn't load the list of Orders, error # " + status;
            console.log("error");
    });


}

You do not need annotations {{}} 您不需要注释{{}}

 ng-init="getImages(data)

and then inside the controller 然后在控制器内部

$scope.getImages = function(data){
 console.log(data.id);
}

You need to keep one thing in mind, If you are want your angular code to work on html page, use {{}}. 您需要记住一件事,如果您想让角度代码在html页面上工作,请使用{{}}。 And you do not need these curly braces if you are writing the code with the angular attributes... It means if something is already part of angularjs, you need not to use curly braces. 而且,如果您正在编写带有angular属性的代码,则不需要这些花括号。这意味着,如果某些东西已经成为angularjs的一部分,则无需使用花括号。

To solve this issue, you need to replace the below line 要解决此问题,您需要替换以下行

<div class="row" ng-init="getImages({{data.id}})">

with this following line 与下面这行

<div class="row" ng-init="getImages(data.id)">

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

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