Angularjs 无法从DOM单击更新“$scope”索引值

Angularjs 无法从DOM单击更新“$scope”索引值,angularjs,angularjs-directive,Angularjs,Angularjs Directive,在我的指令中,我有自定义模板并替换为现有模板。当自定义模板单击时,我需要更新$scope.index以进行更新 但它不起作用。但当我安慰这家酒店时,一切都正常。正确的方法是什么 这是我的自定义指令: var myApp = angular.module('myApp', []); myApp.controller('main', function ($scope) { $scope.values = [{"name":"one", "num" : 1}, {"name":"two", "

在我的指令中,我有自定义模板并替换为现有模板。当自定义模板单击时,我需要更新
$scope.index
以进行更新

但它不起作用。但当我安慰这家酒店时,一切都正常。正确的方法是什么

这是我的自定义指令:

var myApp = angular.module('myApp', []);

myApp.controller('main', function ($scope) {

  $scope.values = [{"name":"one", "num" : 1}, {"name":"two", "num" : 2}, {"name":"three", "num" : 3}]

  $scope.index = 0;

  $scope.update = function (num) {
    $scope.index = num;
  }

});

myApp.directive("newArray", function ($compile) {

  return {

    scope : {
      value : "=",
      index : "=",
      update:"&"
    },

    link : function (scope, element, attrs) {

      var getTemplate = function (val, index) {

        switch(index) {

          case 0 :
            return $('<div />', {
              class:'red',
               html : "<h1>testing</h1>",
              click : function () {
                console.log(scope.value.num); //works
                scope.update(scope.value.num); //not wroking
              }
            });
            break;

            case 1 :
            return $('<div />', {
              class:'blue',
              html : "<h1>testing</h1>",
              click : function () {
                scope.update(scope.value.num);
              }
            });
            break;

            case 2 :
            return $('<div />', {
              class:'green',
              html : "<h1>testing</h1>",
              click : function () {
                scope.update(scope.value.num);
              }
            });
            break;

        }

      }

      element.html(getTemplate(scope.value, scope.index));
      $compile(element.contents())(scope);
      element.replaceWith(element.contents());

    }

  }

})
var myApp=angular.module('myApp',[]);
myApp.controller('main',函数($scope){
$scope.values=[{“name”:“one”,“num”:1},{“name”:“two”,“num”:2},{“name”:“three”,“num”:3}]
$scope.index=0;
$scope.update=函数(num){
$scope.index=num;
}
});
指令(“newArray”,函数($compile){
返回{
范围:{
值:“=”,
索引:“=”,
更新:“&”
},
链接:函数(范围、元素、属性){
var getTemplate=函数(val,索引){
开关(索引){
案例0:
返回$(''){
类别:'红色',
html:“测试”,
单击:函数(){
console.log(scope.value.num);//有效
scope.update(scope.value.num);//未写入
}
});
打破
案例1:
返回$(''){
类别:'蓝色',
html:“测试”,
单击:函数(){
scope.update(scope.value.num);
}
});
打破
案例2:
返回$(''){
类别:'绿色',
html:“测试”,
单击:函数(){
scope.update(scope.value.num);
}
});
打破
}
}
html(getTemplate(scope.value,scope.index));
$compile(element.contents())(范围);
替换为(element.contents());
}
}
})

在html中更改行

<new-array index='$index' update="update" value='value' ng-repeat="value in values">{{value.name}}</new-array>

最后,改变:

$scope.update = function (num) {
    $scope.index = num;
  }


请参见

作为第一次单击,我收到错误。。。我的代码有什么问题?你说的“第一次点击,我就出错了”是什么意思?你有没有试过那玩意儿。它在我的浏览器中运行良好。我已经修改了你的第二项和第三项的代码。在我的实际项目中,我没有得到结果。当单击dive i am console上的update函数时,它会返回类似“$get.d.(匿名函数)(”),但不会调用该函数。。要想解决这个问题,你必须提供一些示例代码供我分析。是否在正确的位置调用了$scope.$apply()?这是必需的,因为来自jquery的click事件在angularjs摘要周期之外。这是我的
控制器和
指令以及
html
文件。你能看看这个吗?
scope.update(scope.value.num);
scope.update({num: scope.value.num});
$scope.update = function (num) {
    $scope.index = num;
  }
 $scope.update = function (num) {
    $scope.index = num;
    $scope.$apply();
  }