首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularJS -指示双向绑定不适用于孤立范围

AngularJS -指示双向绑定不适用于孤立范围
EN

Stack Overflow用户
提问于 2014-08-14 04:34:43
回答 1查看 16.1K关注 0票数 2

我正在构建一个演示天气应用程序,通过使用控制器/提供者&2指令(带有独立的作用域,一个用于显示周值预测的指令,另一个指令用于在页面顶部显示单击的工作日预测)。

在一周中的某一天,我试图在屏幕上显示被点击的工作日。--如果我移除指令中的孤立作用域,这是很好的,但是它不适用于孤立作用域。

有人能告诉我我在这里错过了什么吗?

链接:具有独立作用域的指令:http://plnkr.co/edit/L9AcMv?p=preview ()--不工作吗?我在这里遗漏了什么?)

代码供您参考:

代码语言:javascript
复制
    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'
      };
    });

非常感谢,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-14 05:01:49

使用隔离作用域时,控制器作用域的setSelectedItem方法是不可见的。

解决方案:

向指令等量作用域添加一个setSelectedItem。+还添加了双向数据绑定来进行预测。

工作地点:http://plnkr.co/edit/e5ApIg?p=preview

所作的修改如下:

代码语言:javascript
复制
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上

代码语言:javascript
复制
<div my-weather place="place" forecast="forecast"></div>
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25300163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档