2016-02-12 12 views
2

我正在開發一個基於用戶選項的解決方案。如何重新加載下拉改變的角度指令/組件?

用戶可以有3個配置文件。基於該選擇,X,Y和Z應該根據該配置文件重新加載/更新新數據。

頁面保持不變,只有X,Y和Z應該重新加載。 我該怎麼做?我怎樣才能使這些「元素」在任何變化後重新加載?

enter image description here

+0

我會說,使用'$ rootScope $ emit'上下拉的變化和每個控制器的連接監聽器.. –

+0

我認爲這將有助於你[代碼](http://jsfiddle.net/ftfish/KyEr3/) –

回答

1

一個簡化的解決方案可以沿着這一做法的東西:

app.directive('x', function($rootScope) { 
    return { 
    link: function(scope, element) { 
     $rootScope.$on('profile:changed', function(event) { 
     scope.template = event.data.templateUrl; 
     } 
    } 
    template: '<div ng-include="template">' 
    } 
}); 
app.controller('AppCtrl', function($scope) { 
    $scope.choseTemplate = function(template) { //fire when dropdown selection changed 
    $scope.emit('profile:changed', {templateUrl : template}); 
    }; 
}); 
+0

我讀過的地方,它不建議使用「鏈接」功能。是對的嗎? – skd

+0

根據文檔:當您不需要將控制器暴露給其他指令時,請使用鏈接功能。如果你不需要它,你可以省略「元素」變量。當然,如果你願意,你可以使用控制器。 –

+0

我已經使用了組件,但它與您的示例指令非常相似。謝謝 – skd

-1

通過<select>標籤產生的下拉列表?如果是這樣你可以使用這個方法:

<select ng-options="item as item.name for item in items" 
ng-model="selectedItem" ng-change="update()"></select> 

然後當一個新的選擇拾取您的更新功能將火,你可以更新。

$scope.update = function() { 
    $scope.item = //your code 
} 
+0

你甚至沒有在你的答案中使用組件 –