Cis*_*idx 3 angularjs sails.js
我将两个变量传递给我的视图.如何从角度控制器访问此变量?
风帆控制器:
return res.view('dashboard', {
me: {
id: user.id,
apiKey: user.connection.apiKey
}
});
Run Code Online (Sandbox Code Playgroud)
角度控制器:
angular.module('DashboardModule').controller('DashboardController', function($scope, $http) {
$http.get("/api/" + encodeURIComponent($scope.me.apiKey));
.success(function(response) {$scope.records = response.records;});
}
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<h5>Api Key:</h5>
<p ng-model="me.apiKey"><%= me.apiKey %></p>
<ul>
<li ng-repeat="x in records">
{{ x.guiLoginName }}
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我正在尝试对特定的apiKey执行GET,这是一个变量.这个变量可以从EJS访问,但不是有角度的.我究竟做错了什么?公平地说,我从根本上不明白帆与角有什么联系......
Sails不与Angular通信,就像那样简单.Sails是前端不可知的.
您可以将locals变量渲染到模板中,因为在调用时会传递模板,res.view()但基本上会设置<%= me.apiKey %>块.
你能做的是:
在您的模板中:
<script>
var apiKey = <%= me.apiKey %>;
</script>
Run Code Online (Sandbox Code Playgroud)
然后在Angular你可以恢复它们:
app.controller('DashboardController', ['$scope', '$window',
function ($scope, $window) {
$scope.apiKey = $window.apiKey;
}
]);
Run Code Online (Sandbox Code Playgroud)