未检测到角度变化

时间:2014-10-02 08:29:03

标签: javascript angularjs

变量字符串的更改无法正常工作

var angulargap = angular.module("angulargap", []);
angulargap.factory('cartService', function($rootScope,$http){    
    var fac ={
        message:"factory",
        getCart:function(call){
            $http.post("/rpc.php", {app:"get_cart",pag:"cart"})
            .success(function(data, status, headers, config) {
                fac.setMessage("success");      
            }).error(function(data, status, headers, config) {

            });

        },
        setMessage:function(m){fac.message=m;} 
    }
    return fac; 
});
angulargap.controller("iridium", function ($scope,cartService){     
    cartService.getCart();  
    $scope.message=cartService.message;
});

相反,更新数组一切正常 怎么了 ? 感谢

2 个答案:

答案 0 :(得分:0)

替换此行

 setMessage:function(m){fac.message=m;} 

 setMessage:function(m){message=m;} 

答案 1 :(得分:0)

我通过用对象

更改字符串变量来解决它
var angulargap = angular.module("angulargap", []);
angulargap.factory('cartService', function($rootScope,$http){    
    var fac ={
        messages:{cartnum:"0"},
        getCart:function(call){
            $http.post("/rpc.php", {app:"get_cart",pag:"cart"})
            .success(function(data, status, headers, config) {
                fac.setCounter("num from server");      
            }).error(function(data, status, headers, config) {

            });

        },
        setCounter:function(m){fac.messages.cartnum=m;} 
    }
    return fac; 
});
angulargap.controller("iridium", function ($scope,cartService){     
    cartService.getCart();  
    $scope.messages=cartService.messages;
});

现在一切正常,我在前端看到了结果 如果我使用字符串变量,有人可以向我解释为什么它不起作用吗?

我明白了:

{{messages.cartnum}}