2015-07-21 40 views
0

我在訪問範圍變量在我的HTML文件{{模型}}語法錯誤:意外標記{在指令角JS

在我的HTML文件中得到一個錯誤

<div shipping-Detail-Directive type="billingAddress"> </div> 

這裏是我的要求 我希望用戶填寫'type'屬性中的值,我必須在我的ShippingDetailDirective.html文件中訪問它。我知道我在訪問指令模型時出了點問題。

在我ShippingDetailDirective.html

<div class="order-content shipping-cost-confirm" ng-repeat="x in order"> 


      <div><span>Name</span><label>:</label><span>{{x.{{model}}.firstName}} {{x.{{model}}.lastName}}</span></div> 
      <div><span>Company</span><label>:</label><span>{{x.{{model}}.companyName}}</span></div> 
      <div><span>Address</span><label>:</label><span>{{x.{{model}}.address}}</span></div> 
      <div><span>City</span><label>:</label><span>{{x.{{model}}.city}}</span></div> 
      <div><span>State/Province</span><label>:</label><span>{{x.{{model}}.state}}</span></div> 
      <div><span>Zip Code</span><label>:</label><span>{{x.{{model}}.zipCode}}</span></div> 
      <div><span>Country</span><label>:</label><span>{{x.{{model}}.country}}</span></div> 
      <div><span>Office Tel</span><label>:</label><span>{{x.{{model}}.officeTelephone}}</span></div> 
      <div><span>Home Tel</span><label>:</label><span>{{x.{{model}}.homeTelephone}}</span></div> 
      <div><span>Fax</span><label>:</label><span>{{x.{{model}}.fax}}</span></div> 
      <div><span>Mobile Phone</span><label>:</label><span>{{x.{{model}}.mobileNo}}</span></div> 
      <div><span>E-mail Address</span><label>:</label><span>{{x.{{model}}.email}}</span></div> 
      <div class="edit-forgot float-r o-view-btn"> 
       <input type="button" ui-sref="app.Billing" value="EDIT ADDRESS" class="white-bg"></div> 
</div> 

,這裏是我的指令

app.directive('shippingDetailDirective', function() { 
    return{ 
     scope: { 
     model: '=' 
     }, 
     templateUrl: 'templates/ShippingDetailDirective.html', 
     controller: 'ShippingCtrl', 
      link : function (scope, elem, attrs, controller) { 
       scope.model = attrs.type; 

      } 
    } 

}); 

因此,誰能告訴我,有沒有其他的方法來做到這一點。

回答

0
app.directive('shippingDetailDirective', function($parse) { 
return { 
    scope: { 
    model: '=' 
    }, 
    templateUrl: 'templates/ShippingDetailDirective.html', 
    controller: 'ShippingCtrl', 
    link: function (scope, elem, attrs, controller) { 
    scope.model = $parse(attrs.type)(scope); 
    } 
}; 
}); 
0
  1. 我不認爲你需要在一個孤立的範圍「模式」進行綁定。所需要的只是:

    app.directive('shippingDetailDirective', function() { 
    return{ 
        scope: true, 
        templateUrl: 'directive.html', 
        controller: 'ShippingCtrl', 
        link : function (scope, elem, attrs, controller) { 
         scope.model = attrs.type; 
        } 
        } 
    }); 
    
  2. 爲什麼使用{{x.{{model}}.firstName}}{{model}}打印「billingAddress」,因此,如果您wnat訪問屬性「billingAddress」的範圍變量x那麼所有你需要做的就是

    {{x.model.firstName}}

  3. 您收到此「意外的標記{」錯誤becaue您使用{{}}{{}}

+0

我已經使用的東西,但它也不工作,並給予null值。你能告訴我有沒有其他的方式來做到這一點。其實我從ShippingCtrl控制器獲取訂單值。 –

+0

你在用什麼東西。什麼是問題?看到這個plnkr,如果它可以幫助你:http://plnkr.co/edit/i2WGkTlHQf6OIG50iPGt?p=preview –

1

我猜你想{{x[model].firstName}}更換{{x.{{model}}.firstName}},但我們不得不看到你的數據就知道了。

+0

感謝您的建議這項工作 –

0

通過使用{{x [model] .firstName}}我的程序從服務器獲取數據。

但問題是,

<div shipping-Detail-Directive type="billingAddress"> </div> 

<div shipping-Detail-Directive type="shippingAddress"> </div> 

兩個billingAddress和shippingAddress具有相同的密鑰值,但不同 數據,但這裏的shippingAddress數據獲取覆蓋billingAddress數據。兩者顯示相同的數據,因爲它們都具有相同的範圍模型 我可以使用if(attr.type =='')條件根據其類型屬性來定義不同的scope.variable。但我不想這樣做。 我想要一個不同的方法,如果它是可能的