如何通知范围在angularjs中更新?

时间:2015-07-07 07:04:31

标签: javascript angularjs angularjs-scope

这是我的HTML: -

<div ng-app="myApp">
<!-- this controller will be in different page -->
    <table ng-controller="customersCtrl">
        <tr ng-repeat="x in names">
            <td><a ng-href="#/edit/{{x.id}}">{{ x.name }}</a></td>
            <td>{{ x.description }}</td>
        </tr>
    </table>
<!-- this controller will be in different page -->
    <div id="login" ng-controller='sign_up'>
        <input type="text" size="40" ng-model="name"><br>
        <input type="text" size="40" ng-model="description"><br>
        <button ng-click="check_credentials()">Login</button><br>
        <span id="message"></span>
    </div>
</div>

问题: - 我有两个不同的控制器,如果 sign_up 控制器更新,我该如何自动更新 customerCtrl

这是我的javascript代码: -

var app = angular.module('myApp', [])
    .controller('customersCtrl', function($scope, $http) {
        $http.get("exp.php").success(function(response) {
            $scope.names = response;
        })
    })
    .controller('sign_up', function ($scope, $http) {
        $scope.check_credentials = function () {
            var request = $http({
                method: "post",
                url: window.location.href + "save.php",
                data: {
                    name: $scope.name,
                    description: $scope.description
                },
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            });
            request.success(function (data) {
                document.getElementById("message").textContent = "You have login successfully with name " + data;
            });
        }
    })

1 个答案:

答案 0 :(得分:0)

您可以使用手表,广播或发射。