angularjs – 角度ng点击与调用控制器功能不工作
我需要传递一个变量到另一个控制器。我有以下代码与ListCtrl相关:
<a href="#items" data-router="article" ng-click="changeListName('metro')"> 链接将转到另一个控制器,ItemCtrl。 我想传递一个变量到ItemCtrl。我想到使用一个名为SharedProperties的服务: service('sharedProperties',function () { var list_name = ''; return { getListName: function() { return list_name; },setListName: function(name) { list_name = name; } }; }); 当链接被点击时,我调用一个角度点击事件来触发以下功能: $scope.changeListName = function(name) { sharedProperties.setListName(name); }; 但是,ng点击似乎没有更改我的共享属性的值… 更新 我在ng-click触发的函数中放置一个警报,并触发警报,因为它应该是。 但是,当我写我的功能,像这样: $scope.changeListName = function(name) { sharedProperties.setListName(name); }; 它不工作…它看起来像’sharedProperties.setListName(name);’未执行… 如果我把它外面的函数与虚名称(例如。metro),它的工作原理.. 更新3 我试过多个东西,我很自信,问题是与这个功能: $scope.changeListName = function(list_name) { sharedProperties.setListName(list_name); }; 你有什么想法为什么这种情况发生吗?
你应该使用
ngHref指令和ngClick:
<a ng-href='#here' ng-click='go()' >click me</a> 这里是一个例子:http://plnkr.co/edit/FSH0tP0YBFeGwjIhKBSx?p=preview <body ng-controller="MainCtrl"> <p>Hello {{name}}!</p> {{msg}} <a ng-href='#here' ng-click='go()' >click me</a> <div style='height:1000px'> <a id='here'></a> </div> <h1>here</h1> </body> var app = angular.module('plunker',[]); app.controller('MainCtrl',function($scope) { $scope.name = 'World'; $scope.go = function() { $scope.msg = 'clicked'; } }); 我不知道这是否将与您正在使用的库,但它会至少让你链接和使用ngClick函数。 **更新** 这里是一个演示的集合,得到工作罚款与服务。 http://plnkr.co/edit/FSH0tP0YBFeGwjIhKBSx?p=preview var app = angular.module('plunker',function($scope,sharedProperties) { $scope.name = 'World'; $scope.go = function(item) { sharedProperties.setListName(item); } $scope.getItem = function() { $scope.msg = sharedProperties.getListName(); } }); app.service('sharedProperties',function () { var list_name = ''; return { getListName: function() { return list_name; },setListName: function(name) { list_name = name; } }; }); *编辑* 请回顾https://github.com/centralway/lungo-angular-bridge谈论如何使用lungo和角。另请注意,如果您的网页在浏览到其他链接时完全重新加载,则需要将共享属性保留到localstorage和/或cookie中。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |