Angular中的可变范围

And*_*rew 4 html angularjs

我正在Angular中构建一个简单的网页注册表单,将数据存储在Parse中.

controller('AppCtrl', ['$scope', '$rootScope', function($scope, $rootScope) {

    $rootScope.groupCreated = false;

    $scope.signUp = function(form) {

        var Group = Parse.Object.extend("AdminOrg");
        var group = new Group();
        group.set("groupName", form.name);
        group.set("groupPassword", form.password);

            group.save(null, {
                  success: function(data) {
                      $rootScope.groupCreated = true;

                  },
                  error: function(data, error) {
                    alert(error.message);
                  }
                });
      };
}]);
Run Code Online (Sandbox Code Playgroud)

我使用$ rootScope.groupCreated在我的HTML隐藏注册表格,并显示出"集团成功创建"消息,一旦成功,函数被调用.该值已成功更改为true,但它不会更改html视图.我使用以下隐藏和显示两个div:

ng-hide="$rootScope.groupCreated"
ng-show="$rootScope.groupCreated"
Run Code Online (Sandbox Code Playgroud)

我在HTML中错误地访问了$ rootScope吗?

dfs*_*fsq 5

Well Angular只是不知道你在范围上改变了一些东西,因为Parse函数不是Angular生态系统的一部分,所以不包括在摘要检查循环中.只需使用$ apply方法手动知道Angular :

group.save(null, {
    success: function (data) {
        $rootScope.groupCreated = true;
        $rootScope.$apply();
    },
    error: function (data, error) {
        alert(error.message);
    }
});
Run Code Online (Sandbox Code Playgroud)

另一个问题,感谢vp_arth在注释中注意到:你不在$rootScopeAgnular表达式中使用前缀:

ng-show="groupCreated"
Run Code Online (Sandbox Code Playgroud)