Angularjs 关于范围更改的更新指令

Angularjs 关于范围更改的更新指令,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,我正在更新控制器中的$scope变量 socket.on('data', function(data){ //do some processing; //loop through $scope object; //update $scope.abc.xyz object }); 在指令中,我监视此对象的更改。这是指令中的一个狙击手 app.module('some.mod', []) .directive('custom',['$compile', '$http', '$template

我正在更新控制器中的
$scope
变量

socket.on('data', function(data){
 //do some processing;
 //loop through $scope object;
 //update $scope.abc.xyz object
});
在指令中,我监视此对象的更改。这是指令中的一个狙击手

app.module('some.mod', [])
.directive('custom',['$compile', '$http', '$templateCache', 'someService', function($compile, $http, $templateCache, someService){
var getTemplate = function(a, b, c){
// Some Logic 
// Determine Template URL
templateLoader = $http.get(templateUrl, {cache: $templateCache});
return templateLoader;

}
var linker = function(scope, element, attrs, ctrl, transclude){
    ** Some more logic and black magic **

    loader = getTemplate(a,b,c);

    loader.success(function(html) {
            element.html(html);
        }).then(function (response) {
            element.replaceWith($compile(element.html())(scope));
        });
    scope.$watch('_b', function(){
      console.log("Changed");
     // How do I reload the template/directive
    },true);

};

return {
        restrict: 'E',
                    transclude: true,
        scope : {
            _a : '=abc',
            _b : '=def',
            _c : '=xyz'
        },
        link: linker
    };

}]);

当数据更改时,我确实看到了console.log,但找不到在模板/指令中反映这些更改的方法

您需要使用
$scope.$apply()
内部
。on('data')
fn来运行摘要循环
app.module('some.mod', [])
.directive('custom',['$compile', '$http', '$templateCache', 'someService', function($compile, $http, $templateCache, someService){
var getTemplate = function(a, b, c){
// Some Logic 
// Determine Template URL
templateLoader = $http.get(templateUrl, {cache: $templateCache});
return templateLoader;

}
var linker = function(scope, element, attrs, ctrl, transclude){
    ** Some more logic and black magic **

    loader = getTemplate(a,b,c);

    loader.success(function(html) {
            element.html(html);
        }).then(function (response) {
            element.replaceWith($compile(element.html())(scope));
        });
    scope.$watch('_b', function(){
      console.log("Changed");
     // How do I reload the template/directive
    },true);

};

return {
        restrict: 'E',
                    transclude: true,
        scope : {
            _a : '=abc',
            _b : '=def',
            _c : '=xyz'
        },
        link: linker
    };

}]);