简体   繁体   English

在 Angular 指令中将函数作为参数传递不起作用

[英]Passing a function as parameter in an Angular Directive doesn't work

I'm new in angular and I'm trying to connect two isolated directives under the same controller.我是 angular 新手,我试图在同一个控制器下连接两个独立的指令。 My project is about a product store;我的项目是关于一个产品商店; the first directive shows all available products in a list with a button, the second shows the information with details.第一个指令在一个带有按钮的列表中显示所有可用产品,第二个指令显示带有详细信息的信息。 Workflow should be this: when I click on the button of a product the detail information should be reloaded with the content of the choosen product.工作流程应该是这样的:当我点击一个产品的按钮时,详细信息应该与所选产品的内容一起重新加载。

I think I've done all the conections needed, but it still doesn't work, when I click the button nothing happens... Here are my declarations:我想我已经完成了所有需要的连接,但它仍然不起作用,当我点击按钮时没有任何反应......这是我的声明:

  • Main.html主文件

    <div ng-controller="ProductController as productCtrl"> <product-list data=productCtrl.productList></product-list> <product-details title={{productCtrl.activeProduct.title}} img={{productCtrl.activeProduct.imgSrc}} activator="productCtrl.setActiveProduct(p)"></product-details> <div>
  • ProductList.js产品列表.js

     'use strict' angular.module('angularTestAppApp') .directive("productList", function(){ return { restrict: 'E', scope:{ data: "=", function: "&activator" }, templateUrl: 'views/ProductList.html' } });
  • ProductList.html产品列表.html

     <table class="table"> <thead> <tr> <th></th> <th>Name</th> <th>Price</th> <th></th> </tr> </thead> <tbody> <tr ng-repeat="product in data"> <th scope="row"> <button class="btn btn-info glyphicon glyphicon-play" ng-click='function(product)'></button> </th> <td>{{product.title}}</td> <td>{{product.price | currency}}</td> <td> <img ng-src={{product.imgSrc}} width=15%></img> </td> </tr> </tbody> </table>
  • ProductController.js产品控制器.js

     'use stricts' angular.module('angularTestAppApp') .controller('ProductController', function(productListService) { ... this.activeProduct = { "title": "Hymn for the Weekend", "imgSrc": "images/hymn.jpg", "price": "2" }; this.setActiveProduct = function(p) { this.activeProduct = p; console.log('Active product ' + this.activeProduct); } });

Any idea?任何的想法?

Thanks for all :)!谢谢大家:)!

EDIT: The problems were: - 1: The argument was in the wrong directive.编辑:问题是: - 1:参数在错误的指令中。 - 2: I was writing bad the function, to set parameters you need to bind the data by this way: - 2:我写的函数不好,设置参数需要通过这种方式绑定数据:

    ng-click='function({product:product})'

Instead of 

    ng-click='function(product)'

Calling in the HTML directive by this way:

    <product-list data=productCtrl.productList activator="productCtrl.setActiveProduct(product)"></product-list>

Thanks for your help :).谢谢你的帮助 :)。

There are 2 issues,有2个问题,

  • You are not passing function="expression" to the production-list directive您没有将function="expression"传递给production-list指令
  • The & expression works on the scope, so the function must be defined in the scope &表达式作用于作用域,所以函数必须在作用域中定义

 'use strict'; angular.module('angularTestAppApp', []) angular.module('angularTestAppApp') .directive("productList", function() { return { restrict: 'E', scope: { data: "=", function: "&activator" }, template: '<table class="table"><thead><tr><th></th><th>Name</th><th>Price</th><th></th></tr></thead><tbody><tr ng-repeat="product in data"><th scope="row"><button class="btn btn-info glyphicon glyphicon-play" ng-click="function({product:product})"></button></th><td>{{product.title}}</td><td>{{product.price | currency}}</td><td><img ng-src={{product.imgSrc}} width=15%></img></td></tr></tbody></table>' } }); angular.module('angularTestAppApp') .controller('ProductController', function($scope) { this.productList = [{ title: 1, price: 1 }, { title: 2, price: 2 }]; this.activeProduct = { "title": "Hymn for the Weekend", "imgSrc": "images/hymn.jpg", "price": "2" }; var ctrl = this; $scope.setActiveProduct = function(p) { ctrl.activeProduct = p; console.log('Active product ' + ctrl.activeProduct, ctrl); } });
 <link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="angularTestAppApp"> <div ng-controller="ProductController as productCtrl"> <product-list data="productCtrl.productList" activator="setActiveProduct(product)"></product-list> <pre>{{productCtrl.activeProduct}}</pre> <product-details title={{productCtrl.activeProduct.title}} img={{productCtrl.activeProduct.imgSrc}} activator="productCtrl.setActiveProduct(p)"></product-details> </div> </div>

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

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