AngularJs在没有任何事件的情况下调用指令方法

时间:2013-12-04 11:40:40

标签: angularjs angularjs-directive

我有一个指令:

myDirectives.directive('myRating', function () {
return {
  restrict: 'A',
  template: '<div> <ul style="margin : 0px">' +
              '<li ng-repeat="i in getNumber(myNumber)" ng-click="toggle($index)"              id=$index readonly="false">' + '<img src="star.png">'                  
              '</li>' +
            '</ul></div>',

  scope: {
    .
    .
    .
  },
  link: function (scope, elem, attrs) {       
       // if myId exists in array tab[]           
       if (tab.indexOf(scope.myId+"") != -1) {
          //get a value val

          toggle(val);   //*******Call function toggle ***********
        }

    scope.toggle= function(val) {
          scope.ratingValue = val + 1;          
          // set clicked rating value and change ui as well
    }

现在,错误是=&gt;未定义切换。
虽然代码也可以在这里重复,但为了避免冗余,有没有办法可以调用切换? 感谢

1 个答案:

答案 0 :(得分:0)

正如CodeHater所回答的那样,scope.toggle(做了诀窍......标记答案......谢谢
但这引发了一个新的错误......随后在Load value from db on rendering directive template