我可以同时将AngularJS模型绑定到哈希URL和HTML输入吗?

时间:2014-06-17 18:37:25

标签: angularjs

我正在教自己Angular,我查看了一些示例,展示了如何将模型绑定到HTML输入,以便它们始终包含相同的文本。

据我所知,Angular还提供了与URL配合使用的$ location服务。

我有一个应用程序,我正在考虑将Angular中的部分重写作为一个学习示例。

在我的示例中,我有一个HTML输入,我使用jQuery与模型同步,并且还与哈希URL同步。

使用AngularJS有一种简单的方法吗?

考虑下面的示例应用程序:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/angular.js"></script>
    <script>
        var myApp = angular.module('myApp', []);

        function FirstController($scope, $location) {
            var data = {
                bar: 'hello world'
            };
            $scope.data = data            
        }
    </script>
</head>
<body>
    <div ng-app="myApp">

        <div ng-controller="FirstController">
            <input ng-model="data.bar" />           
            <h2>{{ data.bar }}</h2>
        </div>
    </div>
</body>
</html>

这是一个简单的示例,展示了如何使模型与文本框保持同步。我想知道是否可以让它与哈希网址保持同步,这样我们就可以http://www.example.com#bar=What_The_User_Typed

1 个答案:

答案 0 :(得分:0)

您可能需要的是$ routeProvider

https://docs.angularjs.org/tutorial/step_07