Angularjs 更新DOM时重新启动初始化

Angularjs 更新DOM时重新启动初始化,angularjs,Angularjs,我有一页: <div>111</div><div id="123" ng-init='foo=555'>{{foo}}</div> 代码js refresh id=123并获取新的html。我得到: <div id="123" ng-init='foo="444new"'><span>..</span><b>NEW TEXT<b> {{foo}}</div> 我想进入浏览器

我有一页:

<div>111</div><div id="123" ng-init='foo=555'>{{foo}}</div>
代码js refresh id=123并获取新的html。我得到:

<div id="123" ng-init='foo="444new"'><span>..</span><b>NEW TEXT<b> {{foo}}</div>
我想进入浏览器:

111    
555
111
...NEW TEXT 444new
在这种情况下是否可以重新运行初始化程序

演示:

我的解决方案:-这是一个坏习惯

两个月后UPD:天哪,我写的都是些废话(

请尝试下一步:

$scope.$apply(function() {
  // your js updates here..
});

查看页面底部。

尝试下一步:

$scope.$apply(function() {
  // your js updates here..
});

查看页面底部。

尝试下一步:

$scope.$apply(function() {
  // your js updates here..
});

查看页面底部。

尝试下一步:

$scope.$apply(function() {
  // your js updates here..
});


请查看页面底部。

查看随附代码和

angular不会注册数据绑定或指令的两个原因是元素未编译,或者更改发生在angular之外。使用$compile服务,指令中的
compile
函数和
$scope.$apply
是解决方案。有关用法,请参阅下文

示例标记:

<div my-directive></div>
<div my-directive2></div>
<button id="bad-button">Bad Button!</button>
var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
  $scope.foo = '123!';
  $scope.bar = 'abc!';

  //this is bad practice! just to demonstrate!
  var badButton = document.getElementById('bad-button');
  badButton.addEventListener('click', function() {
    //in here, the context is outside of angular, so use $apply to tell Angular about changes!
    $scope.$apply($scope.foo = "Foo is changed!");   
  });
});

app.directive('myDirective', function($compile) {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {
      //when using a link function, you must use $compile on the element
      var newElem = angular.element('<div>{{foo}}</div>');
      element.append(newElem);
      $compile(newElem)(scope);
      //or you can use:
      //$compile(element.contents())(scope);
    }

  };
});

app.directive('myDirective2', function($compile) {
  return {
    restrict: 'A',
    compile: function(element, attrs) {
      //compile functions don't have access to scope, but they automatically compile the element
      var newElem = angular.element('<div>{{bar}}</div>');
      element.append(newElem);
    }
  };
});

坏按钮!
示例代码:

<div my-directive></div>
<div my-directive2></div>
<button id="bad-button">Bad Button!</button>
var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
  $scope.foo = '123!';
  $scope.bar = 'abc!';

  //this is bad practice! just to demonstrate!
  var badButton = document.getElementById('bad-button');
  badButton.addEventListener('click', function() {
    //in here, the context is outside of angular, so use $apply to tell Angular about changes!
    $scope.$apply($scope.foo = "Foo is changed!");   
  });
});

app.directive('myDirective', function($compile) {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {
      //when using a link function, you must use $compile on the element
      var newElem = angular.element('<div>{{foo}}</div>');
      element.append(newElem);
      $compile(newElem)(scope);
      //or you can use:
      //$compile(element.contents())(scope);
    }

  };
});

app.directive('myDirective2', function($compile) {
  return {
    restrict: 'A',
    compile: function(element, attrs) {
      //compile functions don't have access to scope, but they automatically compile the element
      var newElem = angular.element('<div>{{bar}}</div>');
      element.append(newElem);
    }
  };
});
var-app=angular.module('myApp',[]);
应用程序控制器('myCtrl',函数($scope){
$scope.foo='123!';
$scope.bar='abc!';
//这是糟糕的做法!只是为了证明!
var badButton=document.getElementById('bad-button');
badButton.addEventListener('click',function(){
//在这里,上下文在angular之外,所以使用$apply告诉angular有关更改的信息!
$scope.$apply($scope.foo=“foo已更改!”);
});
});
app.directive('myDirective',函数($compile){
返回{
限制:“A”,
链接:函数(范围、元素、属性){
//使用链接函数时,必须在元素上使用$compile
var newElem=angular.element({{foo}}');
元素。追加(newElem);
$compile(newElem)(范围);
//或者您可以使用:
//$compile(element.contents())(范围);
}
};
});
app.directive('myDirective2',函数($compile){
返回{
限制:“A”,
编译:函数(元素、属性){
//编译函数没有访问作用域的权限,但它们会自动编译元素
var newElem=angular.element({{bar}}');
元素。追加(newElem);
}
};
});
根据您的评论进行更新 写这篇文章让我有些畏缩,但这正是让代码正常工作所需要的

var elem = document.getElementById('123');
elem.innerHTML = "<div ng-init=\"foo='qwe123'\">{{foo}}</div>";
$scope.$apply($compile(elem)($scope));
var elem=document.getElementById('123');
elem.innerHTML=“{{foo}}”;
$scope.$apply($compile(elem)($scope));
正如我所说的,您需要编译元素,因为它位于事件侦听器中,所以您还需要使用
$apply
,以便Angular了解您正在进行的编译


也就是说,如果你要做这样的事情,你真的需要学习更多关于angular的知识。任何类似的事情都应该通过指令来完成,而不是通过任何直接的DOM操作来完成。

请参阅随附代码和

angular不会注册数据绑定或指令的两个原因是元素未编译,或者更改发生在angular之外。使用$compile服务,指令中的
compile
函数和
$scope.$apply
是解决方案。有关用法,请参阅下文

示例标记:

<div my-directive></div>
<div my-directive2></div>
<button id="bad-button">Bad Button!</button>
var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
  $scope.foo = '123!';
  $scope.bar = 'abc!';

  //this is bad practice! just to demonstrate!
  var badButton = document.getElementById('bad-button');
  badButton.addEventListener('click', function() {
    //in here, the context is outside of angular, so use $apply to tell Angular about changes!
    $scope.$apply($scope.foo = "Foo is changed!");   
  });
});

app.directive('myDirective', function($compile) {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {
      //when using a link function, you must use $compile on the element
      var newElem = angular.element('<div>{{foo}}</div>');
      element.append(newElem);
      $compile(newElem)(scope);
      //or you can use:
      //$compile(element.contents())(scope);
    }

  };
});

app.directive('myDirective2', function($compile) {
  return {
    restrict: 'A',
    compile: function(element, attrs) {
      //compile functions don't have access to scope, but they automatically compile the element
      var newElem = angular.element('<div>{{bar}}</div>');
      element.append(newElem);
    }
  };
});

坏按钮!
示例代码:

<div my-directive></div>
<div my-directive2></div>
<button id="bad-button">Bad Button!</button>
var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
  $scope.foo = '123!';
  $scope.bar = 'abc!';

  //this is bad practice! just to demonstrate!
  var badButton = document.getElementById('bad-button');
  badButton.addEventListener('click', function() {
    //in here, the context is outside of angular, so use $apply to tell Angular about changes!
    $scope.$apply($scope.foo = "Foo is changed!");   
  });
});

app.directive('myDirective', function($compile) {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {
      //when using a link function, you must use $compile on the element
      var newElem = angular.element('<div>{{foo}}</div>');
      element.append(newElem);
      $compile(newElem)(scope);
      //or you can use:
      //$compile(element.contents())(scope);
    }

  };
});

app.directive('myDirective2', function($compile) {
  return {
    restrict: 'A',
    compile: function(element, attrs) {
      //compile functions don't have access to scope, but they automatically compile the element
      var newElem = angular.element('<div>{{bar}}</div>');
      element.append(newElem);
    }
  };
});
var-app=angular.module('myApp',[]);
应用程序控制器('myCtrl',函数($scope){
$scope.foo='123!';
$scope.bar='abc!';
//这是糟糕的做法!只是为了证明!
var badButton=document.getElementById('bad-button');
badButton.addEventListener('click',function(){
//在这里,上下文在angular之外,所以使用$apply告诉angular有关更改的信息!
$scope.$apply($scope.foo=“foo已更改!”);
});
});
app.directive('myDirective',函数($compile){
返回{
限制:“A”,
链接:函数(范围、元素、属性){
//使用链接函数时,必须在元素上使用$compile
var newElem=angular.element({{foo}}');
元素。追加(newElem);
$compile(newElem)(范围);
//或者您可以使用:
//$compile(element.contents())(范围);
}
};
});
app.directive('myDirective2',函数($compile){
返回{
限制:“A”,
编译:函数(元素、属性){
//编译函数没有访问作用域的权限,但它们会自动编译元素
var newElem=angular.element({{bar}}');
元素。追加(newElem);
}
};
});
根据您的评论进行更新 写这篇文章让我有些畏缩,但这正是让代码正常工作所需要的

var elem = document.getElementById('123');
elem.innerHTML = "<div ng-init=\"foo='qwe123'\">{{foo}}</div>";
$scope.$apply($compile(elem)($scope));
var elem=document.getElementById('123');
elem.innerHTML=“{{foo}}”;
$scope.$apply($compile(elem)($scope));
正如我所说的,您需要编译元素,因为它位于事件侦听器中,所以您还需要使用
$apply
,以便Angular了解您正在进行的编译


也就是说,如果你要做这样的事情,你真的需要学习更多关于angular的知识。任何类似的事情都应该通过指令来完成,而不是通过任何直接的DOM操作来完成。

请参阅随附代码和

angular不会注册数据绑定或指令的两个原因是元素未编译,或者更改发生在angular之外。使用$compile服务,指令中的
compile
函数和
$scope.$apply
是解决方案。有关用法,请参阅下文

示例标记:

<div my-directive></div>
<div my-directive2></div>
<button id="bad-button">Bad Button!</button>
var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
  $scope.foo = '123!';
  $scope.bar = 'abc!';

  //this is bad practice! just to demonstrate!
  var badButton = document.getElementById('bad-button');
  badButton.addEventListener('click', function() {
    //in here, the context is outside of angular, so use $apply to tell Angular about changes!
    $scope.$apply($scope.foo = "Foo is changed!");   
  });
});

app.directive('myDirective', function($compile) {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {
      //when using a link function, you must use $compile on the element
      var newElem = angular.element('<div>{{foo}}</div>');
      element.append(newElem);
      $compile(newElem)(scope);
      //or you can use:
      //$compile(element.contents())(scope);
    }

  };
});

app.directive('myDirective2', function($compile) {
  return {
    restrict: 'A',
    compile: function(element, attrs) {
      //compile functions don't have access to scope, but they automatically compile the element
      var newElem = angular.element('<div>{{bar}}</div>');
      element.append(newElem);
    }
  };
});

坏按钮!
示例代码:

<div my-directive></div>
<div my-directive2></div>
<button id="bad-button">Bad Button!</button>
var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
  $scope.foo = '123!';
  $scope.bar = 'abc!';

  //this is bad practice! just to demonstrate!
  var badButton = document.getElementById('bad-button');
  badButton.addEventListener('click', function() {
    //in here, the context is outside of angular, so use $apply to tell Angular about changes!
    $scope.$apply($scope.foo = "Foo is changed!");   
  });
});

app.directive('myDirective', function($compile) {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {
      //when using a link function, you must use $compile on the element
      var newElem = angular.element('<div>{{foo}}</div>');
      element.append(newElem);
      $compile(newElem)(scope);
      //or you can use:
      //$compile(element.contents())(scope);
    }

  };
});

app.directive('myDirective2', function($compile) {
  return {
    restrict: 'A',
    compile: function(element, attrs) {
      //compile functions don't have access to scope, but they automatically compile the element
      var newElem = angular.element('<div>{{bar}}</div>');
      element.append(newElem);
    }
  };
});
var-app=angular.module('myApp',[]);
应用程序控制器('myCtrl',函数($scope){
$scope.foo='123!';
$scope.bar='abc!';
//这是糟糕的做法!只是为了证明!
var badButton=document.getElementById('bad-button');
badButton.addEventListener('click',function(){
//在这里,上下文在angular之外,所以使用$apply告诉angular关于ch