【问题标题】:When click button value not update in angular js当单击按钮值未在角度 js 中更新时
【发布时间】:2015-12-03 00:08:54
【问题描述】:

这个html代码:

<md-button class="md-primary " ng-click="setDevice('phone')">Phone           
        </md-button>
<md-button class="md-primary " ng-click="setDevice('tablet')">
           Tablet
        </md-button>

<div class="area-{{setDevice}}">
    <me-iphone tmp-url="{{appTemplateUrl}}"></me-iphone>
</div>

第一次点击手机或平板按钮时更新setDevice值

$scope.setDevice = "";
$scope.setDevice = function(setDevice){
            $scope.setDevice = setDevice;
        }

问题是(首先点击手机),当点击平板按钮时没有更新setDevice值

点击两个按钮时,希望更新$scope.setDevice

【问题讨论】:

    标签: html angularjs node.js


    【解决方案1】:

    像这样使用你的代码:

    $scope.setDevice = function(setDevice){
       return setDevice;
    }
    

    【讨论】:

      【解决方案2】:

      虽然,你的思路是正确的,但你在语法上有点搞砸了。 :) 这没关系,也是学习中必不可少的一步。

      在您的代码中,您首先将setDevice 声明为字符串,然后声明为函数。因为最后一个赋值是一个函数,所以你的$scope.setDevice不再是一个字符串,而是一个函数。

      把变量名改成别的就行了。

      $scope.currentDevice = "";
      $scope.setDevice = function(setDevice){
          $scope.currentDevice = setDevice;
      }
      

      在你的HTML

      <md-button class="md-primary " ng-click="setDevice('phone')">Phone</md-button>
      <md-button class="md-primary " ng-click="setDevice('tablet')">Tablet</md-button>
      
      <div class="area-{{currentDevice}}"> <!-- THIS NEEDS TO BE UPDATED AS WELL -->
          <me-iphone tmp-url="{{appTemplateUrl}}"></me-iphone>
      </div>
      

      【讨论】:

      • 我正要回答几乎相同的新帖子。 +1
      • 更新“currentDevice”值但不刷新 div 标签。我该怎么办?
      • But not refresh div tag 是什么意思? Angular 有 2 路数据绑定。您不需要更新/刷新标签 (DOM) :)
      • 你是对的。我仔细检查了我的代码,有语法错误。我修好了。它现在正在工作。谢谢你提醒我这个关于 Angular js 的想法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      相关资源
      最近更新 更多