如何从其他控制器更改角度模型值
How to change angular model value from other controller
这里是小角度代码。
<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="personCtrl">
<button ng-click="toggle()">Hide user</button>
<p ng-hide="myVar">
First Name: <input type=text ng-model="firstName"><br>
Last Name: <input type=text ng-model="lastName"><br><br>
Full Name: {{firstName + " " + lastName}}
</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.firstName = "John",
$scope.lastName = "Doe"
$scope.myVar = false;
$scope.toggle = function() {
$scope.myVar = !$scope.myVar;
}
});
</script>
</body>
所以当我们在文本框中更改值时,更改会反映在标签中。假设我还有一个叫Test的按钮。我想当用户点击测试按钮时,通过ng js代码,我将通过其他ng控制器代码更改名字文本框中的值。如果可能的话,请帮我处理代码。感谢
我将提供另一种方法。
DO使用工厂操作模型。
不要使用广播,因为你的应用程序在一个更大的scle上会有应用程序范围内的braodcast无缘无故地到处拍摄。
如果您以这种方式构建应用程序,您将更容易操作模型并确保它们在应用程序中同步。它最终会让测试和堆栈跟踪在未来变得更容易。
注意:我公开了一种点击更新模型的方法,但这并不重要,我这样做主要是为了向你展示如何在工厂中操作所有的模型更改,如果需要的话,只需使用非常小的控制器来触发更改。
示例HTML:
<html>
<body ng-app="app">
<div ng-controller="testCtrl as test">
<input type="text" ng-model="test.model.name">
</div>
<div ng-controller="testCtrl2 as test2">
<input type="text" ng-model="test2.model.name">
</div>
<script type="text/javascript" src="https://code.angularjs.org/1.2.26/angular.min.js"></script>
<script type="text/javascript" src="app.js"></script>
</body>
</html>
示例app.js
(function(){
"use strict";
angular.module('app',[]);
function testCtrl( testFactory ){
var testCtrl = this;
testCtrl.model = testFactory.model;
testCtrl.send = testFactory.updateName;
}
function testCtrl2( testFactory ){
var testCtrl2 = this;
testCtrl2.model = testFactory.model;
}
function testFactory(){
var testFactory = this;
testFactory.model = {
name: "test"
};
testFactory.updateName = function( newValue ){
testFactory.model.name = newValue;
}
return testFactory;
}
angular.module('app')
.controller('testCtrl', ['testFactory', testCtrl])
.controller('testCtrl2', ['testFactory', testCtrl2])
.factory('testFactory', [testFactory]);
})();
如果你想要一个很好的最佳实践教程,请从以下内容开始:http://toddmotto.com/opinionated-angular-js-styleguide-for-teams/
<html>
<body ng-app="app">
<div ng-controller="testCtrl">
<input type="text" ng-model="name">
<button ng-click="send()" >Send</button>
</div>
<div ng-controller="testCtrl2">
<input type="text" ng-model="name">
</div>
<script type="text/javascript" src="https://code.angularjs.org/1.2.26/angular.min.js"></script>
<script type="text/javascript" src="app.js"></script>
</body>
</html>
---app.js------------------
var app= angular.module('app',[]);
app.controller('testCtrl',function($scope,$rootScope){
$scope.name = "test";
$scope.send = function(){
$rootScope.$broadcast('sendEvent', { any: $scope.name });
});
app.controller('testCtrl2',function($scope, $rootScope){
$rootScope.$on('sendEvent', function(event, args) {
$scope.name = args.any;
});
相关文章:
- 如何从其他控制器更改角度模型值
- 挖空、视图模型位于其他对象和数据绑定中
- ng显示基于其他一些html元素的模型值
- backbone.js在其他模型中渲染/创建多个模型
- Aurelia组件在其他视图模型中使用时不共享实例
- Ember.js:一个模型如何观察其他模型
- 当可观察数组是其他模型的属性时,挖空不会更新 UI
- 角度ng模型依赖于形式上的其他ng模型
- 如何将外部 API 中的其他数据添加到集合中的现有模型
- 主干.js:如何在集合/其他模型中实例化模型
- 可由其他视图模型访问的挖空可观察/视图模型
- 点击模型并添加到主干中的其他集合中
- 为什么Ember模型的命名与其他模型不同
- 使用干净的url将模型传递给chaplinjs中的其他控制器
- 如何在角度js中使用模型的值作为其他模型的名称
- 当指令的其他实例中的模型发生更改时,检查函数
- Ember.js渲染模板在其他模板中没有获取模型
- 将数据模型设置为其他数据模型的属性
- 如何在backbone.js中将视图/模型的引用传递给其他视图/模型/路由?
- v模型上的输入,动态地改变其他脚本的值