我需要将一个变量传递给另一个控制器.我有以下与ListCtrl相关的代码:
<a href="#items" data-router="article" ng-click="changeListName('metro')">
Run Code Online (Sandbox Code Playgroud)
该链接将转到另一个控制器ItemCtrl.
我想将一个变量传递给ItemCtrl.我想过使用一个名为SharedProperties的服务:
service('sharedProperties', function () {
var list_name = '';
return {
getListName: function() {
return list_name;
},
setListName: function(name) {
list_name = name;
}
};
});
Run Code Online (Sandbox Code Playgroud)
单击链接时,我调用角度单击事件来触发以下功能:
$scope.changeListName = function(name) {
sharedProperties.setListName(name);
};
Run Code Online (Sandbox Code Playgroud)
但是,ng-click似乎不会更改我的共享属性的值...
UPDATE
我在由ng-click触发的函数内部发出一个警报,并触发警报,因为它应该是.
但是,当我写这样的函数时:
$scope.changeListName = function(name) {
sharedProperties.setListName(name);
};
Run Code Online (Sandbox Code Playgroud)
它不起作用......它看起来像'sharedProperties.setListName(name);' 没有执行......
如果我把它放在具有虚拟名称的函数之外(例如,地铁),它可以工作..
更新3
我尝试了很多东西,我很有信心问题是这个功能:
$scope.changeListName = function(list_name) {
sharedProperties.setListName(list_name);
};
Run Code Online (Sandbox Code Playgroud)
你知道为什么会这样吗?
luc*_*uma 104
您应该使用ngHref指令和ngClick:
<a ng-href='#here' ng-click='go()' >click me</a>
Run Code Online (Sandbox Code Playgroud)
这是一个例子: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';
}
});
Run Code Online (Sandbox Code Playgroud)
我不知道这是否适用于您正在使用的库,但它至少会让您链接并使用ngClick功能.
**更新**
这是一个集的演示,并通过服务正常工作.
http://plnkr.co/edit/FSH0tP0YBFeGwjIhKBSx?p=preview
var app = angular.module('plunker', []);
app.controller('MainCtrl', 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;
}
};
});
Run Code Online (Sandbox Code Playgroud)
*编辑*
请查看https://github.com/centralway/lungo-angular-bridge,其中讨论了如何使用lungo和angular.另请注意,如果在浏览到其他链接时页面完全重新加载,则需要将共享属性保存到localstorage和/或cookie中.
在角度配置中定义 Controller 时使用别名。例如:注意:我在这里使用 TypeScript
请注意控制器,它有一个别名 homeCtrl。
module MongoAngular {
var app = angular.module('mongoAngular', ['ngResource', 'ngRoute','restangular']);
app.config([
'$routeProvider', ($routeProvider: ng.route.IRouteProvider) => {
$routeProvider
.when('/Home', {
templateUrl: '/PartialViews/Home/home.html',
controller: 'HomeController as homeCtrl'
})
.otherwise({ redirectTo: '/Home' });
}])
.config(['RestangularProvider', (restangularProvider: restangular.IProvider) => {
restangularProvider.setBaseUrl('/api');
}]);
}
Run Code Online (Sandbox Code Playgroud)
这是使用它的方法......
ng-click="homeCtrl.addCustomer(customer)"
Run Code Online (Sandbox Code Playgroud)
试试吧..它可能对你有用,因为它对我有用......;)
| 归档时间: |
|
| 查看次数: |
437656 次 |
| 最近记录: |