在过去的几天里,我一直在继续研究 angularjs,尤其是关于指令的。
我有一个场景,我必须加载一个记录列表,每个记录都属于一个类别(下拉选择框中的 5 个类别)。我创建了一个指令来处理它们中的每一个的“更改”事件,并使其范围不重叠。正如您在控制台日志中看到的那样,每次从下拉列表中选择一个项目时,它都独立于其他项目。
我有两个问题:
如何仅从外部资源加载类别一次。更具体地说,我只想调用一次 practiceFactory.getCategories()。
我的代码结构在角度方面是否正确?
这是我的html:
<div data-ng-app="practiceApp">
<h1>practiceAppController</h1>
<div data-ng-controller="practiceAppController">
<p><select data-when-changed="saveCategory({{selectedCategoryId}})" data-ng-model="selectedCategoryId" data-ng-options="category.id as category.name for category in categories"></select></p>
<p><select data-when-changed="saveCategory({{selectedCategoryId}})" data-ng-model="selectedCategoryId" data-ng-options="category.id as category.name for category in categories"></select></p>
<p><select data-when-changed="saveCategory({{selectedCategoryId}})" data-ng-model="selectedCategoryId" data-ng-options="category.id as category.name for category in categories"></select></p>
<p><select data-when-changed="saveCategory({{selectedCategoryId}})" data-ng-model="selectedCategoryId" data-ng-options="category.id as category.name for category in categories"></select></p>
<p><select data-when-changed="saveCategory({{selectedCategoryId}})" data-ng-model="selectedCategoryId" data-ng-options="category.id as category.name for category in categories"></select></p>
</div>
</div>
这是我的js:
var practiceApp = angular.module('practiceApp', []);
practiceApp.controller('practiceAppController', function($scope, practiceFactory) {
});
practiceApp.factory('practiceFactory', function() {
var factory = {};
factory.getCategories = function() { // async call here
console.log('getCategories() was called');
var categories = [
{id: 1, name: 'Food & Dining'},
{id: 2, name: 'Hotel & Travel'},
{id: 3, name: 'Shopping'},
{id: 4, name: 'Health & Beauty'},
{id: 5, name: 'Activities'}
];
return categories;
}
factory.getSelectedCategory = function() { // async call here
console.log('getSelectedCategory() was called');
var selectedCategoryId = Math.floor((Math.random()*5)+1);
return selectedCategoryId;
}
return factory;
});
practiceApp.directive('whenChanged', function(practiceFactory) {
return {
restrict: 'A',
scope: {},
controller: function ($scope) {
$scope.selectedCategoryId = practiceFactory.getSelectedCategory();
$scope.categories = practiceFactory.getCategories();
$scope.saveCategory = function(categoryId) {
console.log(categoryId);
}
},
link: function(scope, element, attribute) {
element.bind('change', function() {
scope.$apply(attribute.whenChanged);
})
}
}
});
这是一个jsfiddle。http://jsfiddle.net/j45fN/