看看angularJS无法正常工作

时间:2014-01-16 09:01:27

标签: angularjs angularjs-directive

我已经在pubInfoKey上编写了监视,但是当我在select函数中将值设置为pubInfoKey时它无法正常工作。这里可能出现什么问题,我想在pubInfoKey值改变上调用Controller changeState方法

指令的链接功能:

link: function(scope, element, attrs, ngModelCtrl, state) {
var pubInfoKey;
scope.$watch('pubInfoKey', function(val) {
    if (val) {
        $scope.changeState(val);
    }
}
);

var autoSuggest = element.autocomplete({

select: function(event, ui) {
    var i = 0;
    for (i = 0; i < scope.sug.length; i++)
    {
        if (scope.sug[i] === ui.item.value.toString())
        {
            pubInfoKey = scope.suggestionKey[i];
            break;
        }
    }
}
});                             
}

控制器:

$scope.sug = [];
$scope.suggestionKey = [];
$scope.changeState = function(value) {
                //some code
            };

1 个答案:

答案 0 :(得分:1)

$ watch检查每个$ digest上的范围属性

  • 当我运行您的代码时,我得到SyntaxError: Unexpected token }
  • 您还需要pubInfoKey成为scope
  • 上的媒体资源
  • 如果自动填充是jQuery插件,那么您需要触发$digest
var autoSuggest = element.autocomplete({
  select: function(event, ui) {
    var i = 0;
    for (i = 0; i < scope.sug.length; i++) {
      if (scope.sug[i] === ui.item.value.toString()) {
        scope.$apply(function(){        
          scope.pubInfoKey = scope.suggestionKey[i];          
        })
        break;
     }
    }
  }
});