我有类似的东西:
<div ng-controller="ControllerA">
<input type="text" id="search_form" value="Search" ng-model="searchModel" />
</div>
<div ng-controller="ControllerB">
<ul>
<li ng-repeat="item in items | filter:searchModel">{{item}}</li>
</ul>
</div>
但是,当我在输入栏中搜索时,它不会影响我的列表。如何使一个控制器的模型影响另一个控制器的内容?
谢谢。
编辑
ControllerA
并且ControllerB
彼此完全隔离,我想保持这种状态。如果需要与其他控制器共享模型,该如何使用$rootScope
?
您可以使用service
来在控制器之间共享数据。使用必须定义角度的工厂特征service
。
这是我在这里通过简单的Google搜索找到的示例。
<!doctype html>
<html ng-app="project">
<head>
<title>Angular: Service example</title>
<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<script>
var projectModule = angular.module('project',[]);
projectModule.factory('theService', function() {
return {
thing : {
x : 100
}
};
});
function FirstCtrl($scope, theService) {
$scope.thing = theService.thing;
$scope.name = "First Controller";
}
function SecondCtrl($scope, theService) {
$scope.someThing = theService.thing;
$scope.name = "Second Controller!";
}
</script>
</head>
<body>
<div ng-controller="FirstCtrl">
<h2>{{name}}</h2>
<input ng-model="thing.x"/>
</div>
<div ng-controller="SecondCtrl">
<h2>{{name}}</h2>
<input ng-model="someThing.x"/>
</div>
</body>
</html>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句