Javascript 角度指令ng重复范围和服务
我正在尝试创建angular指令,该指令可以在没有控制器的情况下单独运行,因此我可以将其设置在我想要的任何位置,而无需将模型添加到控制器。 代码相当简单:Javascript 角度指令ng重复范围和服务,javascript,angularjs,angularjs-directive,angularjs-scope,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,我正在尝试创建angular指令,该指令可以在没有控制器的情况下单独运行,因此我可以将其设置在我想要的任何位置,而无需将模型添加到控制器。 代码相当简单: App.directive('ngdPriceWithCurrencySelect',['CurrenciesService' ,function (CurrenciesService) { return { restrict: 'E', replace: true, scope: tr
App.directive('ngdPriceWithCurrencySelect',['CurrenciesService' ,function (CurrenciesService) {
return {
restrict: 'E',
replace: true,
scope: true,
link: function ($scope, $element, $attrs) {
$scope.currencies = CurrenciesService.getData();
$scope.$watch('currencies', function(newValue, oldValue){
console.log($scope.currencies);
});
},
template: '<select>\n\
<option ng-repeat="currency in currencies">{{currency.cur_name_he}}</option>\n\
</select>'
}
}]);
这里有一个JS提琴来测试它:您的服务返回一个承诺,而不是数据本身。您需要为服务解析承诺时设置一个函数:
link: function ($scope, $element, $attrs) {
// waiting for the service to resolve the promise by using the done method
CurrenciesService.getData().then(function(data) {
$scope.currencies = data;
});
$scope.$watch('currencies', function(newValue, oldValue){
console.log($scope.currencies);
});
}
选中您的服务返回的是承诺,而不是数据本身。您需要为服务解析承诺时设置一个函数:
link: function ($scope, $element, $attrs) {
// waiting for the service to resolve the promise by using the done method
CurrenciesService.getData().then(function(data) {
$scope.currencies = data;
});
$scope.$watch('currencies', function(newValue, oldValue){
console.log($scope.currencies);
});
}
选中您的服务返回的是承诺,而不是数据本身。您需要为服务解析承诺时设置一个函数:
link: function ($scope, $element, $attrs) {
// waiting for the service to resolve the promise by using the done method
CurrenciesService.getData().then(function(data) {
$scope.currencies = data;
});
$scope.$watch('currencies', function(newValue, oldValue){
console.log($scope.currencies);
});
}
选中您的服务返回的是承诺,而不是数据本身。您需要为服务解析承诺时设置一个函数:
link: function ($scope, $element, $attrs) {
// waiting for the service to resolve the promise by using the done method
CurrenciesService.getData().then(function(data) {
$scope.currencies = data;
});
$scope.$watch('currencies', function(newValue, oldValue){
console.log($scope.currencies);
});
}
检查谢谢,你是我的救世主,你是我的救世主,你是我的救世主,你是我的救世主