2017-03-02 155 views
0

我的指令在js文件中有單獨的控制器,它具有需要從調用頁面填充的範圍變量parameterDatabase。我無法找到將價值傳遞給它的方式。將範圍變量傳遞給指令的控制器

<body ng-app="testAPP" ng-controller="ctl"> 
Directive Here 
<my-cust parameterDATABASE="dt"></my-cust> 

<script > 
    APP = angular.module("testAPP",['schedule']); 

    APP.controller("ctl",function($scope) 
        { 
        $scope.dt = {date:"02-03-2017",sDay:"Thu",sTime:"01:00"}; 
    }) // end of controller 

    APP.directive("myCust",function() 
    { 
     return{ 
       scope:{ 
        parameterDATABASE:'=' 
       }, 
       controller:"scheduleCtrl", 
       templateUrl:"templateForDirective.html" 
       } 
    }) 
</script> 

scheduleCtrl也有一個變量parameterDATABASE。

指令的contrller

var APP = angular.module('schedule',[]); 
APP.controller('scheduleCtrl',function($scope,$filter) 
{ $scope.parameterDATABASE=[]; // This is the variable I want to populate 

..............

+0

對你的問題有點困惑。如果我理解你的代碼,就好像你的自定義控制器(在另一個文件中,顯然上面沒有圖片)應該在該控制器中作爲'$ scope.parameterDATABASE'提供。那是你在找什麼?如果您也發佈了該代碼,並且可能會澄清預期的內容和實際發生的內容,這將會很有幫助。 –

+0

var APP = angular.module('schedule',[]); APP.controller('scheduleCtrl',function($ scope,$ filter) { $ scope.parameterDATABASE = []; //這是我想要填充的變量 – user3513192

+0

它們需要是同一個角度模塊的一部分開始,你使用了3個模塊:頂部有ng-app =「testAPP」,有一個控制器的是「MyRTO」,指令的控制器有「schedule」,這是行不通的。 –

回答

0

1)根據一些角命名約定的一部分,一個指令的屬性名稱應轉換成camelCase

所以,在html DirectiveparameterDATABASE應該parameter-database

因此,該指令裏面,你應該把它作爲,

scope: { 
     parameterDatabase: '=' 
} 

所以,parameterDatabase映射到==>parameter-database

2)你也可以在兩個地方直接使用parameterdatabase而不用大寫。

例如:parameter-database="dt"html directive

scope: { 
      parameterdatabase: '=' 
    } 

<!DOCTYPE html> 
 
<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
<body ng-app="myApp" ng-controller="ctl"> 
 

 
<isolate-scope-with-controller parameter-database="dt" add="addCustomer()"></isolate-scope-with-controller> 
 

 
<script> 
 
var app = angular.module("myApp", []); 
 

 
app.controller("ctl",function($scope) 
 
        { 
 
        $scope.dt = {date:"02-03-2017",sDay:"Thu",sTime:"01:00"}; 
 
    }) // end of 
 
    
 
    app.directive('isolateScopeWithController', function() { 
 
     
 
    var controller = ['$scope', function ($scope) { 
 
\t \t console.log($scope.parameterDatabase) 
 

 
     }], 
 
     
 
     template = '<h1>I am from directive controller</h1><br><h2>{{parameterDatabase}}</h2>'; 
 
     
 
     return { 
 
      restrict: 'EA', //Default in 1.3+ 
 
      scope: { 
 
       parameterDatabase: '=' 
 
      }, 
 
      controller: controller, 
 
      template: template 
 
     }; 
 
    }); 
 

 

 
</script> 
 

 
</body> 
 
</html>

請執行上面的片段

Here is a working DEMO

+0

@ user3513192,請檢查我的答案。 – Sravan

相關問題