【问题标题】:Angular.js ng-click not workingAngular.js ng-click 不起作用
【发布时间】:2018-04-08 23:16:00
【问题描述】:

ng-click 属性没有触发提供的功能。

HTML:

<div class="row">
        <div style="display:inline-block;width:20%;height:25%;margin:20px;"  ng-repeat="news in dailyNews" >
            <div class="thumbnail" ng-click="showNews({{news.id}})">
                <img src="{{news.image}}" alt="images/noimg.jpg" style="width:100%">
                <div class="caption" >
                    <h3  >{{news.heading}}</h3>
                </div>
            </div>
        </div>
    </div>

路由脚本:

.state('dashboard.user',{
        url:'/user-dashboard',
        templateUrl:'views/user.html',
        controller:'userPanelCtrl'
    })

控制器:

.controller('userPanelCtrl',function($state,$scope,userServices){
            userServices.checkSession();
            $scope.showNews = function(id){
                localStorage.setItem("show-news",id);
            }
        })

我成功获取数据并使用 ng-repeat 将它们加载到 html 页面,但 ng-click 对任何重复的 div 都不起作用。提前致谢。

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    请更改此行:

    <div class="thumbnail" ng-click="showNews({{news.id}})">
    

    到

    <div class="thumbnail" ng-click="showNews(news.id)">
    

    ng-click 是 angularjs 指令,所以你不需要写 news.id 像 {{}} 你可以直接传递,因为它会识别你的函数 showNews 同样它也会识别你的 user.id。

    【讨论】:

    • 不是和上面贴的一样吗?
    • @Sajeetharan 相隔 2 分钟,可能在您提交您的代码时开始编写...另外,更正代码是一件事,您未能解释 为什么 OP 的代码是错误的,这是 OP 实际学习的方式。
    • 认为我发布的下一分钟。非常感谢。
    • @Sajeetharan 。当您的答案弹出时,我正在回答中。我没有看到你的答案,我发布了我的,然后看到了你的。
    【解决方案2】:

    您不必将表达式作为参数传递,只需删除 {{}}

    来自

     <div class="thumbnail" ng-click="showNews({{news.id}})">
    

    收件人

     <div class="thumbnail" ng-click="showNews(news.id)">
    

    【讨论】:

    • 认为我发布的下一分钟。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-31
    • 2018-04-30
    • 1970-01-01
    相关资源
    最近更新 更多