1

我有一个带有“刷新”链接的 AngularJS 测试页面,该链接调用一个函数,作为回报,它会更新模型“customer.name”

但是,“刷新”点击仍然没有改变视图中的 {{customer.name}},我不明白为什么。

这是我的应用程序的内容。

索引.html

<!doctype html>
  <head>
    <title>Test</title>
</head>
  <body ng-app="roaMobileNgApp">


    <script src="scripts/angular.js"></script>

    <script src="scripts/angular-ui.js"></script>
    <link rel="stylesheet" href="scripts/angular-ui.css">

    <div class="container" ng-view=""></div>

    <script src="scripts/app.js"></script>
    <script src="scripts/controllers/main.js"></script>


</body>
</html>

应用程序.js

'use strict';

angular.module('roaMobileNgApp', ['ui'])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  });

main.js

'use strict';

angular.module('roaMobileNgApp')
  .controller('MainCtrl', function ($scope) {


    $scope.customer = {name: '',
                      id: 0};



    $scope.getDetails = function() {
        $scope.customer.name = 'Test';
    };

  });

main.html

    <a href="#" ng-click="getDetails()">Refresh</a>
    <p><input type="text" ng-model="customer.name"> {{ customer.name }} </p>

</div>
4

1 回答 1

4

这可能是因为您通过单击更改地址:

<a href="#" ng-click="getDetails()">Refresh</a>

这会强制 Angular 重新渲染 html 以及控制器。

更改如下:

<a href="javascript:void(0)" ng-click="getDetails()">Refresh</a>
于 2013-08-08T13:48:02.637 回答