Friday 19 January 2018

javascript - Angularjs ng-controller with resolve

You can use the mechanism of the
prototype.


.when('/someUrl', {

template : '
ng-template="some.html">
',
controller: function (data)
{
var pr = this;
pr.data = data;
},

controllerAs: 'pr',
resolve : {
data: ['Service', function
(Service) {
return Service.getData();
}]

}
})
angular.module('myApp')
.controller('MyController',
['$scope', function ($scope) {
$scope.data = $scope.pr.data;
//magic
}

]
);

Now wherever you
want to use


'            ng-controller="MyController">
'

you
need to ensure that there pr.data in the Scope of the calling controller. As an example
uib-modal


var modalInstance =
$modal.open({
animation: true,
templateUrl:
'modal.html',
resolve: {
data: ['Service', function (Service)
{
return Service.getData();
}]
},

controller: function ($scope, $modalInstance, data) {
var pr =
this;
pr.data = data;
pr.ok = function () {

$modalInstance.close();
};
},

controllerAs:'pr',

size:'sm'
});

modal.html


            type="text/ng-template" id="modal.html">
class="modal-body">
ng-controller="MyController">


class="modal-footer">





And
now you can use $scope.data = $scope.pr.data; in
MyController


pr.data is my style. You can rewrite the code
without PR.
the basic principle of working with ng-controller described in
this video rel="nofollow">https://egghead.io/lessons/angularjs-the-dot

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...