问题描述
我正在尝试理解Angularjs中的数据绑定。
I am trying to understand data binding in Angularjs.
我想要做的是在页面之间建立绑定,如果我更改first.html上的输入,数据应该在second.html中自动更改。
What I want to do is establish binding between pages that is if I change the input on first.html, the data should automatically change in second.html.
例如,
这是first.html:
For example,This is first.html:
<div ng-controller="MyCtrl">
<input type="text" ng-model="value"/><br>
{{value}}
<a href="#/second"><input type="submit" value="Second page"/></a>
</div>
并说second.html只有这段代码{{value}}。
and say second.html has only this piece of code {{value}}.
在.js文件中我们有$ routeProvider,它将模板url作为'second.html'&控制器是'MyCtrl'。
and in the .js file we have $routeProvider which takes the template url as 'second.html' & the controller is 'MyCtrl'.
所以控制器是:
MyApp.controller(function($scope){
$scope.value="somevalue";
})
通过上述方式,second.html上的{{value}}获取值somevalue。这是来自控制器的。
By doing the above way the {{value}} on the second.html is getting the value "somevalue". Which is comming from the controller.
但如果我动态更改first.html上的输入值,则second.html上的值不会获得该值。
But if I change the input value dynamically that is on the first.html, the value on the second.html is not getting that value.
我的问题是如何自动将second.html上的值与first.html绑定。
My question is how do I bind the value on second.html with first.html automatically.
要清楚地理解这个问题,假设在first.html上有一个用于输入文本的输入字段和一个提交按钮,那么我想在提交的第二个页面上获取first.html的文本字段的输入值。
To understand the question clearly, Suppose there is an input field for entering text and a submit button on first.html, then I want to get the Input value of the text field of the first.html on the second.html page on Submit.
推荐答案
找到我想要的解决方案,解决方案在Angular文档中,这里是链接。
Found the Solution to what I was looking for, The solution is in the Angular docs, here is the link http://docs.angularjs.org/cookbook/deeplinking.
该链接示例的某些部分回答了我的问题。
Some part of the example on that link answers my question.
这篇关于Angularjs中页面之间的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!