[英]How do I pass an asynchronous variable from controller to a directive in angularjs
我是angular的新手,正在努力將通過HTTP資源在控制器中檢索到的變量傳遞給指令。 首先,我有一個問題,即我的ngResource調用是異步的,然后又有一個問題,即我的資源調用已鏈接。
這是我的HTML
<html ng-app="routingRulesApp">
<body ng-controller="RulesDisplayCtrl">
<my-customer info="$activeRuleSetId"></my-customer>
</body>
</html>
這是我的Javascript
var routingRulesApp = angular.module('routingRulesApp', [
'routingRulesControllers',
'routingRulesServices',
'routingRulesDirectives'
]);
var routingRulesControllers = angular.module('routingRulesControllers', []);
routingRulesControllers.controller('RulesDisplayCtrl', ['$scope', 'RuleSets', '$q',
function($scope, RuleSets, $q) {
var fr = null;
var rpromise = $q.defer();
$scope.activeRuleSetId = RuleSets.active({ruleId: 1}, function(activeRuleSetId) {
var ruleSetId = activeRuleSetId[0];
var ruleSet = RuleSets.query({ruleSetId: ruleSetId}, function(ruleSet) {
console.log(ruleSet);
fr = ruleSet;
rpromise.resolve(fr);
}, function(response) {
//404 or bad
if(response.status === 404) {
console.log("HTTP Error", response.status);
}
});
}, function(response) {
//404 or bad
if(response.status === 404) {
console.log("HTTP Error", response.status);
}
});
$scope.formattedResults = rpromise.promise;
}
]);
var routingRulesDirectives = angular.module('routingRulesDirectives', []);
routingRulesDirectives.directive('myCustomer', [
function() {
return {
restrict: 'E',
replace: true,
scope: {
formattedResults: '=info'
},
templateUrl: 'templates/currency-group-rule.html',
controller: function($scope) {
console.log($scope.formattedResults);
debugger;
}
};
}
]);
var routingRulesServices = angular.module('routingRulesServices', ['ngResource']);
routingRulesServices.factory('RuleSets', ['$resource',
function($resource){
return $resource('data/routing-ruleset-:ruleSetId.json', {}, {
query: {method:'GET', isArray:true},
active: {method:'GET', isArray: false, url: 'data/rulesets-activeRuleSetId.json', responseType:"text"}
});
}]);
我試圖在我的指令控制器中保留我的$ scope.formattedResults變量,以便可以構建自定義表/網格解決方案,但不確定如何實現。 如您所見,我很失落。 我嘗試使用延遲對象,並希望它將綁定到變量。
好吧,這不是代碼來回答這個問題,但是我需要向您介紹一些角度方面的內容。
為了實現您的目標:
我可以很快想到兩種方法。
現在,您不必忘記一些關鍵點角力。
大概您知道其中大多數,並且可以肯定地使用Google搜索來解決許多SO問題,這些問題的內容可以解釋上述內容,並且可能會更好。 也許有人也可以給您一些與代碼相關的答案。
祝好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.