2016-02-27 54 views
0

我是新來AngularJS,我是想一個代碼ng-repeat但卡住了,因爲我需要的角度表達傳遞給一個函數,但它是不低於工作是我的代碼:傳遞角表達的功能

HTML代碼

 <div ng-controller="Profile as pro"> 
     <ul class="nav nav-tabs"> 
     <li ng-repeat="tabs in tabArray" 
      ng-class="{active: pro.tab==1}" 
      ng-click=" pro.setTab({{tabs.value}})"> 
     <a href="">{{tabs.name}}</a> 
     </li> 
     </ul> 
    <div> 

控制器代碼

controller.controller('Profile', ['$scope',function ($scope) { 
     $scope.tabArray = [ 
      {name:"Profile","value":1}, 
      {name:"Education","value":2}, 
      {name:"Work","value":3}, 
      {name:"About","value":4} 
     ]; 
     this.tab=1; 
     this.setTab = function(tabSelected){ 
      this.tab=tabSelected; 
     }; 
    }]); 

回答

0

在你的代碼只有proble是以下行:

ng-click=" pro.setTab({{tabs.value}})" // don't use {{ and }} thing here, it is not needed 

下面的代碼是固定的版本:

ng-click="pro.setTab(tabs.value)" // only {{ and }} are removed and it works well 

快樂幫助!

1

使用ng-click ="pro.setTab(tabs.value)"

0

有在現有的代碼中的一些錯誤 - >

  1. 既然你正在使用的ControllerAs語法,你應該與用於controllervariable,是指所有的變量引用在視圖中。

在這裏,你的模型的ng-repeat通過pro.tabArray參考。

  • 沒有必要通過在AngularExpressions的參數,在功能/方法調用的時間。

  • 因此,利用ng-click="pro.setTab(tabs.value)",沒有表情。

    HTML代碼:

    <li ng-repeat="tabs in pro.tabArray" ng-class="{'active': pro.tab==1}" ng-click="pro.setTab(tabs.value)"><a href="">{{tabs.name}}</a></li> 
    
  • 在你JS過,你已經提到了一些varibales與$scope和一些與this。與你的代碼一致,不要混淆。

  • JS代碼:

    var app = angular.module('app', []); 
    
    app.controller('Profile', function ($scope) { 
        this.tabArray = [ 
           { name: "Profile", "value": 1 }, 
           { name: "Education", "value": 2 }, 
           { name: "Work", "value": 3 }, 
           { name: "About", "value": 4 } 
        ]; 
        this.tab = 1; 
        this.setTab = function (tabSelected) { 
         this.tab = tabSelected; 
        }; 
        $scope = this; 
    }); 
    
    +0

    那是一個很好的建議感謝(Y) – shujaat1989