我想做的是:
Process p = new Process();
p.StartInfo = new ProcessStartInfo("/bin/ls", "-l")
{
RedirectStandardOutput = true,
UseShellExecute = false
};
p.Start();
p.WaitForExit();
//The output of the shell command will be in the outPut variable after the
//following line is executed
var outPut = p.StandardOutput.ReadToEnd();
并从http://www.jvandemo.com/how-to-resolve-angularjs-resources-with-ui-router/获取想法)我知道我可以创建一个角度服务来在每个控制器中进行api调用,但如果我在页面上有2个控制器,我不想做出2个相同的请求。
在下面的示例中,我有ui-router
,需要访问nav.controller.js
。
home.config.js
myData
home.controller.js
angular.module('myApp')
.config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('home', {
url: '/',
templateProvider: function($templateCache) {
return $templateCache.get('home.client.view.html');
},
resolve: {
myService: 'myApiService',
myData: function(myApiService){
return myService.get().$promise;
}
},
controller: 'HomeCtrl as home'
});
}
]);
nav.controller.js
angular.module('myApp')
.controller('HomeCtrl', ['myData',
function(myData){
var self = this;
// YAY WORKS!
console.log(myData);
}
]);
another.controller.js
angular.module('myData')
.controller('NavCtrl', ['$scope', 'myData',
function($scope, myData) {
// BOO DOESN'T WORK
console.log(myData);
}
]);
答案 0 :(得分:0)
您仍应使用服务。但是,此服务不应该只进行API调用 - 它还应该存储数据。您的代码看起来像
<强> home.controller.js 强>
angular.module('myApp')
.controller('HomeCtrl', ['myService',
function(myService){
var self = this;
// YAY WORKS!
console.log(myService.myData);
}
]);
<强> nav.controller.js 强>
angular.module('myData')
.controller('NavCtrl', ['$scope', 'myService',
function($scope, myService) {
// BOO DOESN'T WORK
console.log(myService.myData);
}
]);
<强> another.controller.js 强>
angular.module('anotherModule')
.controller('AnotherCtrl', ['$scope', 'myService',
function($scope, myService) {
// BOO DOESN'T WORK
console.log(myService.myData);
}
]);
在myService.get()
函数中,您将返回的数据存储在myData