html - 当点击按钮值在 Angular js中不更新时

标签 html angularjs node.js

此 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

最佳答案

尽管您的思路是正确的,但您在语法上有点困惑。 :) 这是可以的,也是学习中必不可少的一步。

在您的代码中,您首先将 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>

关于html - 当点击按钮值在 Angular js中不更新时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32438613/

相关文章:

javascript - 如果它是外部链接,如何将 rel = "nofollow"添加到 CKEditor 中的链接

javascript - 在返回 promise 的函数上使用 .success 时,未定义不是函数

javascript - "right"处理迟到响应的方法是什么

node.js - Expressjs 和 Body-parser 发布文章

node.js - 我可以使用 sharp 和 multer 来调整图像大小并在一次操作中保存到磁盘吗?

javascript - 在使用 Javascript 进行鼠标悬停时,我有一个单独的 div 更改颜色但我需要在使用后重置该功能?

html - 如何更改eclipse html格式

node.js - 无法更新(卸载/安装)pm2 版本 0.7.8

html - WordPress 生成无效标记,如何删除它?

angularjs - 如何在 Angular Material md-autocomplete 中禁用自动填充