在Angular Directive中传递函数作为参数不会;不起作用
Passing a function as parameter in an Angular Directive doesn't work
我是angular领域的新手,我正试图在同一控制器下连接两个独立的指令。我的项目是关于一个产品商店;第一个指令用一个按钮显示列表中的所有可用产品,第二个指令用详细信息显示信息。工作流程应该是这样的:当我点击产品的按钮时,详细信息应该与所选产品的内容一起重新加载。
我想我已经完成了所有需要的连接,但它仍然不起作用,当我点击按钮时,什么都没有发生。。。以下是我的声明:
-
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>
-
产品列表.js
'use strict' angular.module('angularTestAppApp') .directive("productList", function(){ return { restrict: 'E', scope:{ data: "=", function: "&activator" }, templateUrl: 'views/ProductList.html' } });
-
ProductList.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
'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); } });
知道吗?
谢谢大家:)!
编辑:问题在于:-1:参数的指令错误。-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>
谢谢你的帮助:)。
有2个问题,
- 您没有将
function="expression"
传递给production-list
指令 &
表达式在作用域上工作,因此必须在作用域中定义函数
'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>
相关文章:
- setTimeout刷新,毫秒参数不起作用
- 筛选器's的第二个参数不起作用
- JavaScript - 在函数内连接 2 个参数不起作用
- jQuery 参数不起作用
- 参数不起作用的角度路线
- 为什么此按钮的点击参数不起作用
- ng路由传递参数不起作用
- 数据表后请求参数不起作用
- JSON字符串参数不起作用
- 样式的Javascript函数参数不起作用
- onclick事件的转义参数不起作用
- 参数不起作用的Chrome扩展-js
- Javascript 函数参数不起作用
- 在函数中插入输入值作为参数不起作用
- React-router path参数不起作用
- 角度 UI 路由器 - 状态查询字符串参数不起作用
- 在javascript函数中传递参数不起作用
- Angular的路由和参数不起作用
- 在for中传递onClick的动态参数不起作用
- jQuery中的get url参数不起作用