2015-07-10 38 views
0

我試圖使用一個指令,但是我正面臨着通過布爾值傳遞的問題。當我評價我的網頁,這是我得到的錯誤:在自定義的angularjs指令中使用布爾值

Syntax Error: Token '{' invalid key at column 2 of the expression [{{app.isNew}}] starting at [{app.isNew}}].

我正在使用的代碼如下:

home.html的

<featured-app app-title="{{app.title}}" app-is-new={{app.isNew}}></featured-app> 

家。 js

app.controller('HomeCtrl', [ '$scope', function($scope) { 
    $scope.app.title = "App name"; 
    $scope.app.isNew = true; 
}]); 

featuredApp.js

app.directive('featuredApp', function() { 
    return { 
     templateUrl: 'directives/templates/featuredApp.html', 
     scope: { 
      appTitle: '@', 
      appIsNew: '=' 
     } 
    }; 
}); 

featuredApp.html

<h2>{{appTitle}} <span class="label label-default" ng-show="{{appIsNew}}">New</span></h2> 

有沒有人有,爲什麼我得到上述錯誤的任何想法?我不確定我哪裏出了問題。

回答

0

您的HTML改爲:

home.html的

<featured-app app-title="{{app.title}}" app-is-new="app.isNew"></featured-app> 

或更改featuredApp.js到:

app.directive('featuredApp', function() { 
    return { 
     templateUrl: 'directives/templates/featuredApp.html', 
     scope: { 
      appTitle: '@', 
      appIsNew: '@' 
     } 
    }; 
}); 

以上取決於你的需求。一種是單向綁定,另一種是雙向的。雖然你也必須做以下變化:

featuredApp.html

<h2>{{appTitle}} <span class="label label-default" ng-show="appIsNew">New</span></h2> 
0

替換:

<featured-app app-title="{{app.title}}" app-is-new={{app.isNew}}></featured-app> 

有了:

<featured-app app-title="app.title" app-is-new=app.isNew></featured-app> 

和:

appTitle: '@', 

有了:

appTitle: '=', 

的問題是,你在地方使用{{}}其中角期望一個變量名。