AngularJS 指令未正确绑定属性
AngularJS directive not binding attributes correctly
我对角度比较陌生,这是我第一次参与指令,我有点迷茫。我正在尝试在我的应用程序的单独选项卡上创建一个可重用的项目列表。列表的作用相同,但项目的显示方式除外(由单独的部件处理)。我正在将许多不同类型的属性传递到范围中,并且我正在根据我所阅读的内容尝试几种不同的东西。到目前为止,无论我尝试了什么,我仍然遇到正确绑定属性的问题。
下面是我的代码,我会尽量解释它,希望有人能告诉我我哪里出错了。唯一看起来正确绑定的是字符串,对象和函数丢失。
更新:事实证明我需要将$scope.currentPage绑定到指令范围。现在 ng-repeat 正在运行,但需要访问控制器范围的页面的其他部分不起作用。我已经更新了下面的代码,并继续研究如何授予对模板的访问权限。
命令
var app = angular.module('main');
app.directive('itemList', function(){
var linkFunction = function(scope, element, attributes){
scope.$watch("query.value", function(){
scope.filterFunction(); //pretty sure this never gets called on search
});
}
return {
restrict: 'E',
replace: 'true',
templateUrl: 'partials/directives/list-tab.html',
link: linkFunction,
scope: {
filterFunction: "&filterFunction",
searchPlaceholder: "@searchPlaceholder",
pagedItems: "=pagedItems",
clickFunction: "&clickFunction",
classString: "@classString",
infoTemplate: "@template",
currentPage: "=currentPage"
}
}
});
索引.html
//pagedCars is an array of nested objects that gets used by the template to display the information
//filterCars is a function
//carSelected is a function
<div class="available-items">
<item-list filter-function="filterCars" search-placeholder="Search Cars" paged-items="pagedCars" current-page="currentPage" click-function="carSelected" class-string="car.carId==selectedCar.carId?'selected':''" template="'partials/cars/cars-template.html'"></item-list>
</div>
列表选项卡.html
<div class="form-group">
<div class="search-field">
<label for="searchField" id="searchLabel">Search</label><br/>
<input type="text" ng-model="query.value" placeholder="{{searchPlaceholder}}"/>
</div>
<table class="table table-hover>
<tbody>
//currentPage is on the controller scope there's a separate control that allows the user to page through the pagedItems by updating the currentPage which would be reflected here
<tr ng-repeat="item in pagedItems[currentPage]" ng-click="clickFunction($index) ng-class="classString">
<td ng-include="infoTemplate"></td>
</tr>
</tbody>
</table>
</div>
汽车模板.html
<div class="row form-inline" id="{{item.carId}}">
<div class="col-md-2">
//this uses a method on the controller scope to format the url
<img ng-src="{{retrieveIcon(item.iconUrl)}}" height="75px" width="75px"/>
<div class="col-md-10">
<div details-pane" id="carDetails" ng-include="'partials/cars/car-full-details.html'"></div>
<div class="item-title">{{item.name}}</div>
//the rest is just a table with more information about the item. item.description, item.mileage, etc...
</div>
</div>
尝试使用括号传递函数
<div class="available-items">
<item-list filter-function="filterCars()" search-placeholder="Search Cars" paged-items="pagedCars" click-function="carSelected()" class-string="car.carId==selectedCar.carId?'selected':''" template="'partials/cars/cars-template.html'"></item-list>
</div>
另外仅供参考,如果您的变量在 HTML 中具有与指令范围内所需的相同名称,您可以使用 pass 方法。
scope: {
filterFunction: "&",
searchPlaceholder: "@",
pagedItems: "@",
clickFunction: "&",
classString: "@",
infoTemplate: "@template"
}
相关文章:
- Ember.js/HHandlebars:使用{{view}}助手渲染的视图不绑定属性
- 聚合物 - 在重复模板中获取数据绑定属性值
- 如果指令包含在另一个指令中,我如何在隔离范围内添加双向数据绑定属性
- AngularJS1.2 Directive双向绑定属性未反映到View
- 为什么AngularJS在页面加载时没有自动绑定属性?包括一个非常简单的例子
- 如何在AngularJS指令中绑定属性
- 更改铁列表中现有项的绑定属性的值
- 如何使用 angularjs 中的表达式绑定属性名称
- Angular 指令内部 HTML 重新编译,动态创建绑定属性
- 标识事件中的绑定属性
- AngularJS 指令未正确绑定属性
- 在显示绑定属性之前在 JavaScript 中格式化绑定属性
- 从数据绑定属性迭代键值对
- 剑道绑定属性与字符串和变量
- 如何在一个元素上具有多个数据绑定属性
- 无法识别余烬组件 - 绑定属性渗入控制器
- 挖空添加我自己的数据绑定属性,如 With
- Emberjs如何绑定属性和id
- Ember绑定属性没有更新,尽管控制器属性更新得很好
- 如何在Knockout中选择性地绑定属性