我正在构建一个演示天气应用程序,通过使用控制器/提供者&2指令(带有独立的作用域,一个用于显示周值预测的指令,另一个指令用于在页面顶部显示单击的工作日预测)。
在一周中的某一天,我试图在屏幕上显示被点击的工作日。--如果我移除指令中的孤立作用域,这是很好的,但是它不适用于孤立作用域。
有人能告诉我我在这里错过了什么吗?
链接:具有独立作用域的指令:http://plnkr.co/edit/L9AcMv?p=preview ()--不工作吗?我在这里遗漏了什么?)
代码供您参考:
app.directive('myWeather', function() {
return {
restrict: 'EA',
transclude: 'true',
//If I comment below scope then it works fine though it does not if I am using isolated scope
scope: {
place: '=' //Two-way data binding
},
templateUrl: 'my-weather.html'
};
});
app.directive('selectedWeather', function() {
return {
restrict: 'EA',
transclude: 'true',
//If I comment below scope then it works fine though it does not if I am using isolated scope
scope: {
place: '=' //Two-way data binding
},
templateUrl: 'selected-weather.html'
};
});非常感谢,
发布于 2014-08-14 05:01:49
使用隔离作用域时,控制器作用域的setSelectedItem方法是不可见的。
解决方案:
向指令等量作用域添加一个setSelectedItem。+还添加了双向数据绑定来进行预测。
工作地点:http://plnkr.co/edit/e5ApIg?p=preview
所作的修改如下:
app.directive('myWeather', function() {
return {
restrict: 'EA',
transclude: 'true',
//If I comment below scope then it works fine though it does not if I am using isolated scope
scope: {
place: '=', //Two-way data binding
/// here added a two way data binding to forecast too
forecast: '='
},
templateUrl: 'my-weather.html',
/// here added setSelectedItem to isolated scope.
link: function (scope,e,a) {
scope.setSelectedItem = function(forecast) {
scope.forecast = forecast;
}
}
};
});以及在index.html上
<div my-weather place="place" forecast="forecast"></div>https://stackoverflow.com/questions/25300163
复制相似问题